【问题标题】:Decode Facebook API JSON with PHP使用 PHP 解码 Facebook API JSON
【发布时间】:2014-04-17 18:59:40
【问题描述】:
$url = 'https://api.facebook.com/method/links.getStats?urls=http://google.com&format=json';
$get = json_decode($url, true);
echo 'Shares:' . $get['share_count'];

为什么这不返回任何东西?

【问题讨论】:

  • json_decode 不适用于 URL。您必须下载此请求的响应并将其传递给json_decode

标签: php json facebook api decode


【解决方案1】:

json_decode 不适用于 URL,它需要 string 作为参数。您必须获取此请求的响应并将其传递给json_decode。像这样的:

$url = 'https://api.facebook.com/method/links.getStats?urls=http://google.com&format=json';
$get = json_decode(file_get_contents($url), true);
echo 'Shares:' . $get['share_count'];

【讨论】:

    【解决方案2】:

    您可以使用以下代码获取总数。

    $url = $this->get_json_values( 'https://api.facebook.com/method/links.getStats?urls=http://google.com&format=json' );
    $json = json_decode( $url, true );
    $facebook_count = isset( $json[0]['share_count'] ) ? intval( $json[0]['share_count'] ) : 0;
    

    $facebook_count 将给出 facebook 的总分享数。

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-01
      • 2012-03-22
      • 2018-05-29
      • 2013-07-18
      • 2011-11-10
      • 2019-07-26
      • 1970-01-01
      相关资源
      最近更新 更多