Jac*_*ack 1 php facebook facebook-graph-api facebook-php-sdk
我是PHP和Facebook PHP SDK的新手,我希望'like_count'从'likes' 'summary'Facebook页面帖子中获取.我当前的代码包含以下内容:
$response = $fb->get('/me/posts?fields=admin_creator,likes.limit(0).summary(true)&limit=30');
$getLikeCount = $response->getGraphEdge()->asArray();
foreach($getLikeCount as $likekey){
if(isset($likekey['likes'])){
var_export($likekey['likes']);
foreach ($likekey['likes'] as $likekey){
//echo $likekey['total_count'] . '<br>';
}
}
}
Run Code Online (Sandbox Code Playgroud)
该var_export($likekey['likes']);出口空白阵列,同时var_export($likekey['summary']);返回null.但是,在Graph API Explorer中,它返回以下内容:
{
"admin_creator": {
"name": "NAME",
"id": "ID"
},
"id": "ID",
"likes": {
"data": [
],
"summary": {
"total_count": 1022,
"can_like": true,
"has_liked": false
}
}
},
Run Code Online (Sandbox Code Playgroud)
我如何访问该'total_count'字段,因为通过我的方法访问它'likes'并且" summary'不工作".
编辑:使用getGraphEdge()->asArray();将无法工作,因为它不返回摘要数组.我会以某种方式获取值getDecodedBody();或其他方法.如果我使用$getLikeCount = $response->getDecodedBody();,使用此代码:
foreach($getLikeCount as $key){
if(isset($key['admin_creator'])){
echo $key['admin_creator']['name'];
}
}
Run Code Online (Sandbox Code Playgroud)
它没有返回任何东西.我正在使用它'admin_creator'作为一个例子,因为它$getLikeCount = $response->getGraphEdge()->asArray();在我当前的方法中起作用,但是我不能使用这种方法,因为我试图'total_count'从'summary'post中获取字段,'likes'并且'summary'在使用getGraphEdge()方法时不会在数组中显示仅在使用时显示getDecodedBody();.我想知道是否有办法从中获取值,getDecodedBody()或者是否有解决方法来获取该total_count字段summary.
答案: 答案可以在下面找到.
您可以在不进行更多API调用的情况下找到此数据.摘要数据被隐藏在该领域的元数据的喜欢.评论和反应也是如此.
$response = $fb->get('/me/posts?fields=id,likes.limit(0).summary(true)');
foreach ($reponse->getGraphEdge() as $graphNode) {
// Some fields can have metadata
$likesData = $graphNode->getField('likes')->getMetaData();
echo 'The total of likes for the post ID "' . $graphNode->getField('id') . '" is: '. $likesData['summary']['total_count'];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4463 次 |
| 最近记录: |