【问题标题】:Print Value from JSON URL with php twitter使用 php twitter 从 JSON URL 打印值
【发布时间】:2019-04-25 18:37:25
【问题描述】:

我正在尝试将 json 文件中的值打印到我的网站。 我意识到有一个用于 twitter 的 API 系统,但出于预期目的,我认为不需要申请 twitter api 并等待关注者计数状态。

我不知道为什么。但什么也没有显示。 我有什么遗漏吗?

这是我当前的代码

$json = file_get_contents('https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=stackoverflow');

$data = json_decode($json,true);

$twitcount = $data['followers_count'][0];

echo "<b>";
print_r($twitcount);

访问下面的 url 将推送一个 json 文件,其中包含有关 twitter 帐户的基本信息。 https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=stackoverflow

【问题讨论】:

    标签: php json url twitter


    【解决方案1】:

    如果你运行var_export($data);,你会得到:

    array (
      0 => 
      array (
        'following' => false,
        'id' => '128700677',
        'screen_name' => 'StackOverflow',
        'name' => 'Stack Overflow',
        'protected' => false,
        'followers_count' => 55359,
        'formatted_followers_count' => '55.4K followers',
        'age_gated' => false,
      ),
    )
    

    所以你的代码需要是$twitcount = $data[0]['followers_count'];

    【讨论】:

    • 嗯,有道理,但我仍然有问题。不确定究竟是什么原因造成的。对我来说问题我要么得到 NULL 要么什么都没有。
    【解决方案2】:

    更正你的索引

    数组 ( [0] => 数组 ( [关注] => [id] => 128700677 [screen_name] => StackOverflow [名称] => 堆栈溢出 [受保护] => [followers_count] => 55358 [formatted_followers_count] => 55.4K 关注者 [年龄门控] => )

    )

    $twitcount = $data[0]['followers_count'];

    【讨论】:

    • 即使我做了 var_dump(json_decode($json, true)); 仍然得到 NULL;
    • $json = file_get_contents('cdn.syndication.twimg.com/widgets/followbutton/…); $data = json_decode($json,true); $twitcount = $data[0]['followers_count'];回声'
      ';打印_r($数据); print_r($twitcount);
    • 尝试使用此代码,这将有助于欢呼......并确保您的 json 变量有数据尝试在解码之前回显 $json。
    • 谢谢你所有的答案都是正确的。刚刚与 file_get_contents 发生了一些冲突最终使用了 CURL 解决方案。
    【解决方案3】:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, 'https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=stackoverflow');
    $result = curl_exec($ch);
    curl_close($ch);
    
    
    $data = json_decode($result,true);
    $twitcount = $data[0]['followers_count'];
    print_r($twitcount);

    你所有的答案都是正确的,非常感谢你

    我最终改为使用 CURL 进行设置。 似乎iv在file_get_contents方面遇到了一些问题

    也许是某种冲突。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-02
      • 2013-06-21
      • 1970-01-01
      • 2013-10-20
      • 1970-01-01
      • 2011-08-28
      相关资源
      最近更新 更多