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.