使用cURL创建一个函数-返回cURL数据然后重用


Creating a function using cURL - returning cURL data then reusing?

我想创建一个使用cURL和bit的函数。

我的问题是,我怎么能得到cURL返回的数据字符串,并继续在整个函数中使用它(因为这似乎不起作用,我假设由于尝试return $string,然后在函数的其余部分使用字符串)。

这是我的,它只是显示一个空白页面:

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");

我也尝试过return $short_url并在函数之外回声它,在函数运行后(shorten_url()以下),这也不起作用。

函数将返回NULL。您错过了$string = curl_exec($ch);