【问题标题】:How do a decode the `tweet.json` returned from a twitter API request?如何解码从 twitter API 请求返回的 `tweet.json`?
【发布时间】:2018-11-26 02:31:41
【问题描述】:

如何解码从 twitter API 请求返回的tweet.json
我的 JSON 输出:www.screensystem.nl/twitter.php

PHP:

<?php

require "Twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;

$consumer_key = "";
$consumer_secret = "";
$access_token = "";
$access_token_secret = "";


$connection = new TwitterOAuth($consumer_key, $consumer_secret, $access_token, $access_token_secret);
$content = $connection->get("account/verify_credentials");

$statuses = $connection->get("search/tweets", ["q" => "@TMobile_Webcare"], ["lang" => "NL"]);
//json_encode($statuses);


echo "<pre>";
print_r($statuses);
echo "</pre";

echo $statuses  [0]->text;


?>

更新 17/6

我已经尝试了下面的代码,但我得到一个空白页。我做错了什么?

$connection = new TwitterOAuth($consumer_key, $consumer_secret, $access_token, $access_token_secret);
$content = $connection->get("account/verify_credentials");

$statuses  = $connection->get("search/tweets", ["q" => "@TMobile_Webcare"], ["lang" => "NL"]);

// get your json as an associative array
$jsonArray = json_encode($statuses,true);

// loop through our json array
foreach($jsonArray as $key=>$data) {
            echo $tweet['created_at'];

    // if $data is an array, and you want to loop through it
    foreach($data as $dataKey=>$dataValue) {
        echo $tweet['text'];
            }
}

【问题讨论】:

    标签: php json twitter


    【解决方案1】:

    您需要使用json_encode()。如果您想将它们作为数组而不是 stdObject 获取,请使用 true 作为第二个参数。

    // get your json as an associative array
    $jsonArray = json_encode($statuses,true);
    
    // loop through our json array
    foreach($jsonArray as $key=>$data) {
        // do what you want to here
    
        // if $data is an array, and you want to loop through it
        foreach($data as $dataKey=>$dataValue) {
            // do something here if you needed to
        }
    }
    

    【讨论】:

    • Thkns,我怎样才能用 te 结果制作一个 foreach 循环?
    猜你喜欢
    • 2018-12-02
    • 2012-08-19
    • 2014-09-04
    • 2021-05-28
    • 2017-10-25
    • 2016-08-17
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    相关资源
    最近更新 更多