升级到 PHP 5.4,现在我拉的脸书链接统计数据不再显示在网站上


Upgraded to PHP 5.4 and now facebook link stats I was pulling no longer show on site

我已经经营了一个摄影网站两年多了,并显示了每张图片下方每个图片页面积累了多少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的情况下编写上述内容,如果这是罪魁祸首,可以解决这个问题?

谢谢!

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

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