0

嗨,我对 CURL 有点陌生,但我正在尝试请求一些 json 数据,然后解析结果。我在检索数据方面取得了成功,但我无法处理响应。这是代码

function bitBucketCurl($url)
{
global $bitPassword;
global $bitUsername;

$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$bitUsername:$bitPassword");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
$commitinfo = curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

return $commitinfo;
 }

$json = bitBucketCurl($url); 
echo $json; // This seems to work in that, when I load the page, I can see the json data

//turn json data into an array - this is what does not seem to be working
$obj_a = json_decode($json, true);

print_r ($obj_a); //the result is simply a 1 rather than the array I would expect

基本问题是 json 数据在我出现时出现,echo $json但是当我尝试将该数据转换为数组时它不起作用。当我打印数组时,我只得到一个'1'。

4

1 回答 1

1

我通过添加以下行得到了所需的结果:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
于 2013-03-07T17:06:43.623 回答