2013-01-04 78 views
0

我試圖從Facebook頁面上拉牆貼,但我遇到了問題。我沒有解析JSON的Twitter feed,所以我看不到我遇到了什麼問題。解析PHP中的Facebook JSON時遇到問題

這裏是我的代碼,並here's the tutorial I used for help

<?php 
    $url = "http://www.facebook.com/feeds/page.php?format=json&id=96551536516";    

    function disguise_curl($url) { 
     $curl = curl_init(); 
     $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; 
     $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; 
     $header[] = "Cache-Control: max-age=0"; 
     $header[] = "Connection: keep-alive"; 
     $header[] = "Keep-Alive: 300"; 
     $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
     $header[] = "Accept-Language: en-us,en;q=0.5"; 
     $header[] = "Pragma: "; 

     curl_setopt($curl, CURLOPT_URL, $url); 
     curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla'); 
     curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 
     curl_setopt($curl, CURLOPT_REFERER, ''); 
     curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); 
     curl_setopt($curl, CURLOPT_AUTOREFERER, true); 
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($curl, CURLOPT_TIMEOUT, 10); 

     $html = curl_exec($curl); 
     curl_close($curl); 

     return $html; 
    } 

    $response = json_decode(disguise_curl($url)); 

    foreach($response->entries as $block){ 
     echo 
      "<li class='clearfix'> 
       <div class='streamPosterName'>{$block->author}</div> 
       <div class='postContent'>{$block->title}</div> 
      </li>"; 
    } 
?> 

當我的網頁的其他部分解析JSON,我使用引用JSON對象下面的方法:

<?php 
    $url = "http://search.twitter.com/search.json?q=Apple&rpp=50";    
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    $curlout = curl_exec($ch); 
    curl_close($ch); 
    $response = json_decode($curlout, true); 

    foreach($response["results"] as $block){ 
     echo 
      "<li class='clearfix'> 
       <img src='".$block["profile_image_url"]."' /> 
       <div class='streamPosterName'>".$block["from_user_name"]."</div> 
       <div class='streamPosterUsername'>@".$block["from_user"]."</div> 
       <div class='postContent'>".$block["text"]."</div> 
      </li>"; 
    } 
?> 

上面的代碼正確拉入推文。 Facebook的解析提供下列錯誤:

Catchable fatal error: Object of class stdClass could not be converted to string in /home/public_html/mkt/index.php on line 260

260線如上所示:

<div class='streamPosterName'>{$block->author}</div> 
+1

如果您定義'不工作' – scoota269

+0

Woops,將會有幫助。 – Jon

+1

這只是意味着$ block-> author是Object類型而不是String,因此不會回顯 – Stefan

回答

1
<div class='streamPosterName'>{$block->author}</div> 

默認情況下,json_decode()表示JSON對象作爲PHP stdClass的對象。該錯誤意味着$block->author是一個對象,但您正在使用它,就好像它是一個字符串。可能試試$block->author->name

+0

Ahh dang,愚蠢的錯誤。沒有閱讀通過該JSON對象的所有方式。謝謝! – Jon