2013-02-09 76 views
0

嗨我在CURL有点新,但我试图请求一些json数据,然后解析结果。我在检索数据方面取得了成功,但我无法处理回复。下面的代码使用来自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'。

+0

也似乎加入这一行'curl_setopt($ CH,CURLOPT_RETURNTRANSFER,1);'但是,如果你能解释一下这个是干什么和为什么它很重要,我会很高兴地奖励你回答信用:) – Jeff 2013-02-09 12:33:35

+2

你能告诉我们'$ json'数据吗? – Peter 2013-02-09 12:36:41

+0

'json_encode'的第二个参数接受常量值,'true'几乎不是它期望的。检查[docs](http://php.net/json_encode)处的常量。 – 2013-02-10 11:14:00

回答

1

我加入以下行所需的结果:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);