dongre6073 2016-07-28 23:24
浏览 138

Facebook API获取帖子分享计数

I am trying to get the post share count using the PHP Facebook API (php-sdk-v4).

Right now I can get most of the information that I want, but it looks like the share count is in a different format.

Here's a basic example of my code.

$request = $this->fb->get(
    "/$id/posts?fields=id,message,shares,likes.limit(1).summary(true),comments.limit(1).summary(true)&limit=10"
);
$posts = $request->getGraphEdge();

For the returned $posts I can loop through and retrieve the info that I want for most of the fields using this code:

$id = $post->getField( 'id' );
$message = $post->getField( 'message' );

Likes and Comments are retrieved a little different, but I was able to get it using this:

$likes = $post->getField( 'likes' )->getTotalCount();
$comments = $post->getField( 'comments' )->getTotalCount();

The problem that I am running into is that the shares count is returned as a "GraphNode" Object and I get an error when I try to use getTotalCount. The object $post->getField( 'shares' ) looks like this:

[shares] => Facebook\GraphNodes\GraphNode Object
(
    [items:protected] => Array
    (
        [count] => 1
    )

)

I can not figure out the proper way to retrieve this variable, which in this case would be 1.

Any help would be appreciated. Thank you!

  • 写回答

1条回答 默认 最新

  • duanfei8897 2016-08-04 16:03
    关注

    Try this way, I think it's "proper". it's important to check if $graphNode->getField('shares') is not null before asking for ->getField('count').

    $request = $fb->get('/' . $id . '/posts?fields=id,message,shares,likes.limit(1).summary(true),comments.limit(1).summary(true)&limit=10');
    foreach ($request->getGraphEdge() as $graphNode) {
        // Sometimes "getField" function can return a null value
        if ($graphNode->getField('shares')) {
            $numberShares = $graphNode->getField('shares')->getField('count');
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探