0

我有这段代码:-

<?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

我怎样才能纠正它?

4

2 回答 2

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'});
于 2012-08-13T20:27:59.107 回答
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);
于 2012-08-13T20:31:36.990 回答