简体   繁体   中英

How to print Twitter API request reply?

I want to print all the tweets from the reply. What am I missing?

https://github.com/J7mbo/twitter-api-php

<?php
ini_set('display_errors', 1);
require_once('TwitterAPIExchange.php');

/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
    'oauth_access_token' => "",
    'oauth_access_token_secret' => "",
    'consumer_key' => "",
    'consumer_secret' => ""
);

/** Perform a GET request and echo the response **/
/** Note: Set the GET field BEFORE calling buildOauth(); **/
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=%23twitter';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
             ->buildOauth($url, $requestMethod)
             ->performRequest();

foreach ($response->statuses as $tweet) {
    echo $tweet->text;
}

Notice: Trying to get property of non-object in /var/www/projects/twitter/twitter-api-php/index.php on line 23

Warning: Invalid argument supplied for foreach() in /var/www/projects/twitter/twitter-api-php/index.php on line 23

The performRequest() method returns a JSON string, not an array, so you need to decode it $array = json_decode($response, true); .

You just have to use:

`echo $response;`

or instead:

echo $twitter->setGetfield($getfield) ->buildOauth($url, $requestMethod) ->performRequest();

but I recommend using the first approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM