json解码中的错误 - Facebook

时间:2012-08-13 20:22:28

标签: php facebook facebook-graph-api json

我有这段代码: -

<?php 
$xml = file_get_contents("http://graph.facebook.com/fql?q=SELECT%20url,%20normalized_url,%20share_count,%20like_count,%20comment_count,%20total_count,%20commentsbox_count,%20comments_fbid,%20click_count%20FROM%20link_stat%20WHERE%20url='http://www.facebook.com/nokia'");
echo $xml;
$obj = json_decode($xml);
print $obj;
print $obj->{'like_count'};
?>

它给了我以下错误: -

{"data":[{"url":"http:\/\/www.facebook.com\/nokia","normalized_url":"http:\/\/www.facebook.com\/nokia","share_count":1240,"like_count":8622663,"comment_count":287,"total_count":8624190,"commentsbox_count":2,"comments_fbid":404897301238,"click_count":10}]}
Catchable fatal error: Object of class stdClass could not be converted to string in C:\xampp\htdocs\nokia_app\test.php on line 5

我如何纠正它?

2 个答案:

答案 0 :(得分:2)

在询问这些类型的问题之前,您需要研究API返回的内容。

$xml = file_get_contents("http://graph.facebook.com/fql?q=SELECT%20url,%20normalized_url,%20share_count,%20like_count,%20comment_count,%20total_count,%20commentsbox_count,%20comments_fbid,%20click_count%20FROM%20link_stat%20WHERE%20url='http://www.facebook.com/nokia'");
$obj = json_decode($xml);
var_dump($obj->data[0]->{'like_count'});

答案 1 :(得分:0)

如果您确实需要处理数组中的数据,请尝试以下方法:

class ObjectToArray {
    public static function convert( $object ) {
    if( !is_object( $object ) && !is_array( $object ) ) {
        return $object;
    }

    if( is_object( $object ) ) {
        $object = get_object_vars( $object );
    }

    return array_map( 'ObjectToArray::convert', $object );
     }
}

$aData = ObjectToArray::format($theObject);

print_r($aData);