使用来自Curl响应的json数据

时间:2013-02-09 12:26:29

标签: php json curl

您好我在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

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

1 个答案:

答案 0 :(得分:1)

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

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);