0

我正在尝试从 Facebook 页面中提取墙上的帖子,但我遇到了问题。我在解析 Twitter 提要的 JSON 时没有问题,所以我看不出我遇到了什么问题。

这是我的代码,这是我用于帮助的教程

<?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 解析提供以下错误:

可捕获的致命错误:第 260 行的 /home/public_html/mkt/index.php 中的类 stdClass 的对象无法转换为字符串

第 260 行如上图所示:

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

1 回答 1

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

默认情况下, json_decode() 将 JSON 对象表示为 PHP stdClass 对象。错误表示$block->author是一个对象,但您正在使用它,就好像它是一个字符串一样。

也许试试$block->author->name

于 2013-01-04T15:21:12.640 回答