1

I want to create a function using cURL and the bit.ly API to shorten links.

My question is, how can I get the string of data returned by cURL and continue to use it throughout the function (as this doesn't seem to work, I'd assume due to the attempt to return $string and then use string in the rest of the function).

Here's what I have, which just displays a blank page:

function shorten_url($bit_login,$bit_api,$long_url) {
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "http://api.bitly.com/v3/shorten?login=".$bit_login."&apiKey=".$bit_api."&longUrl=".$long_url."&format=xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);

return $string;

$xml = simplexml_load_string($string);
$short_url = $xml->data[0]->url;
echo $short_url;
}

shorten_url($login,$apikey,"http://www.google.com");

I've also tried to return $short_url and echo it ouside of the function, after the function is run (below shorten_url()), which doesn't work either.

3
  • 1
    Where $string comes from on the "return"? try: $string = curl_exec($ch); Commented Sep 15, 2011 at 22:56
  • I thought the first return after cURL would be the fetched data, such as in - snipplr.com/view/4084. I'll try your suggestion now :). Commented Sep 15, 2011 at 23:05
  • Worked perfectly :), do you want to run that as an answer so I can accept it? Commented Sep 15, 2011 at 23:06

1 Answer 1

5

The function will return NULL. You are missing the $string = curl_exec($ch);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.