【问题标题】:Upgraded to PHP 5.4 and now facebook link stats I was pulling no longer show on site升级到 PHP 5.4,现在我拉的 facebook 链接统计信息不再显示在网站上
【发布时间】:2013-11-02 20:21:56
【问题描述】:

我经营一个摄影网站已经两年多了,并显示了每个图片页面在每张图片下累积的 Facebook 点赞和分享次数的统计数据。

我用来获取此信息并将其放入变量的代码是这样的:

<?php   // Get Facebook Stats for Shares Likes Comments etc...
    $url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=".urlencode($this->canonicalurl);
    $xml = file_get_contents($url);
    $xml = simplexml_load_string($xml);
    $shares = number_format($xml->link_stat->share_count);
    $likes = number_format($xml->link_stat->like_count);
    $comments = number_format($xml->link_stat->comment_count);
    $total = number_format($xml->link_stat->total_count);
?>

一切都很好,直到今天我从 PHP5.2 切换到 5.4,突然所有的统计数据都不再出现。我已经阅读了一些内容,有些人说我需要设置 allow_url_fopen = 1 但已经设置好了。

有人说这可能与使用 file_get_contents 有关,但我没有得到具体结果。

任何人都可以解释一下可能出了什么问题吗?如果这是罪魁祸首,是否有另一种方法可以在不使用 file_get_contents 的情况下编写上述内容?

谢谢!

【问题讨论】:

    标签: php facebook facebook-graph-api file-get-contents json


    【解决方案1】:

    这里的错误是number_format需要是一个double,但是$xml是一个对象。您可以只删除 number_format,但我建议使用 intval。

    <?php   // Get Facebook Stats for Shares Likes Comments etc...
        $url = "http://api.facebook.com/restserver.php?method=links.getStats&urls=".urlencode($this->canonicalurl);
        $xml = file_get_contents($url);
        $xml = simplexml_load_string($xml);
        $shares = intval($xml->link_stat->share_count);
        $likes = intval($xml->link_stat->like_count);
        $comments = intval($xml->link_stat->comment_count);
        $total = intval($xml->link_stat->total_count);
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-03
      • 1970-01-01
      • 2014-04-01
      • 1970-01-01
      • 2015-10-29
      • 1970-01-01
      • 2023-03-08
      • 2015-01-07
      相关资源
      最近更新 更多