新手在这里,我试图获得在我的Facebook页面帖子上发表评论的用户的facebook名称,但它不起作用。我正在使用图API API v2.6。这是我的代码:
$data = $this->check_file_get_contents($this->graph_api.$object_id.'/comments?
fields=from,name&access_token='.$access_token);
//$data = $this->check_file_get_contents($this->graph_api.$object_id.'/&access_token='.$access_token);
$res = json_decode($data);
$uname = $res->name;
echo $uname;
知道这里可能出现什么问题吗?很少的指导会非常有帮助。我错过了什么?
$ object_id是帖子ID。这是图api的输出:
{
"data": [
{
"from": {
"name": "John Doe",
"id": "932242153456808"
},
"id": "585601208257495_620658141418468"
},
{
"from": {
"name": "Jason King",
"id": "10153078961444510"
},
"id": "585601208257495_622535501230732"
},
{
"from": {
"name": "Jason King",
"id": "10153078961444510"
},
"id": "585601208257495_622588581225424"
}
],
"paging": {
"cursors": {
"before": "WTI5dGJXVnVkRjlqZAFhKemIzSTZAOakl3TmpVNE1UUXhOREU0TkRZANE9qRTBOalEyTkRRMU5EZAz0ZD",
"after": "WTI5dGJXVnVkRjlqZAFhKemIzSTZAOakl5TlRnNE5UZA3hNakkxTkRJME9qRTBOalV3TnpVNE9Uaz0ZD"
}
}
}
答案 0 :(得分:0)
试试这个
<?php
$json='{
"data": [
{
"from": {
"name": "John Doe",
"id": "932242153456808"
},
"id": "585601208257495_620658141418468"
},
{
"from": {
"name": "Jason King",
"id": "10153078961444510"
},
"id": "585601208257495_622535501230732"
},
{
"from": {
"name": "Jason King",
"id": "10153078961444510"
},
"id": "585601208257495_622588581225424"
}
],
"paging": {
"cursors": {
"before": "WTI5dGJXVnVkRjlqZAFhKemIzSTZAOakl3TmpVNE1UUXhOREU0TkRZANE9qRTBOalEyTkRRMU5EZAz0ZD",
"after": "WTI5dGJXVnVkRjlqZAFhKemIzSTZAOakl5TlRnNE5UZA3hNakkxTkRJME9qRTBOalV3TnpVNE9Uaz0ZD"
}
}
}';
$res=json_decode($json,true);
$data=$res['data'];
for ($x = 0; $x <= count($data)-1; $x++) {
echo $data[$x]['from']['name']."<br>\n";
}
?>
输出:
John Doe<br>
Jason King<br>
Jason King<br>