如何解码从twitter API请求返回的`tweet.json`?

时间:2018-06-16 15:31:13

标签: php json twitter

如何解码从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'];
            }
}

1 个答案:

答案 0 :(得分:0)

您需要使用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
    }
}