2014-03-06 86 views
2

我試圖從Facebook收集信息以通過Graph API Explorer自動推送到頁面。PHP curl響應採用JSON格式,但var_dump顯示字符串

我有curl請求工作,它給了我有效的JSON(與validator檢查)。

我得到以下錯誤:警告:()提供的foreach無效參數

如果我做了它的var_dump聲明它作爲一個字符串。

PHP:

$curl = curl_init(); 
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1, 
    CURLOPT_URL => $url, 
    CURLOPT_HEADER =>false, 
    CURLOPT_SSL_VERIFYPEER => false, 
    CURLOPT_SSL_VERIFYHOST => false, 
)); 
$response = curl_exec($curl); 
curl_close($curl); 

foreach($response as $posts){ 
    if($posts['from']['id']='230594106985291'){ 
     echo "<div class='contentItem'>"; 
     echo $posts['message']; 
     echo "</div>"; 
    } 
    } 

樣品捲曲響應這裏列出:http://jsfiddle.net/X933V/

回答

5

我想你只需要decode your json作爲數組在它

$response = curl_exec($curl); 
curl_close($curl); 
$json = json_decode($response, true); //note the second parameter true 
            //to have decoded as an associative array 
foreach($json as $posts){ 
+0

你搖滾,我可以」循環前我發誓了,但是我錯了:) – Andrew

+0

歡迎你! – Fabio