Dominic Kirkwood
Dominic Kirkwood

Reputation: 85

Alternative to Twitter's REST API 1.0 user_timeline.json?

I had a file that scrapes twitter search results based on a user's @handle using REST API 1.0 using user_timeline.json:

$json = file_get_contents("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&screen_name=handle&count=25", TRUE);
$twitter_feed = json_decode($json, true);
foreach($twitter_feed as $tweet) {do something with $tweet}

Since REST API v1 is no longer active, I need to replicate the process using V1.1.

I've read through the documentation and understand I now need to auth before running this script. As a beginner, this simple script just got really intimidating.

Once authenticated, what's the best method for returning an array of tweets from a certain user that will imitate the above and return in a nice json array?

Thanks

Upvotes: 0

Views: 509

Answers (2)

Jonas Duerto
Jonas Duerto

Reputation: 29

<?php
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "xxx",  // Access token
'oauth_access_token_secret' => "xxx", // Access token secret
'consumer_key' => "xxx",  // Consumer Key
'consumer_secret' => "xxx" // Consumer secret
);

/** URL for REST request, see: https://dev.twitter.com/docs/api/1.1/ **/
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$requestMethod = "GET";
if (isset($_GET['user']))  {
    $user = $_GET['user'];
} else {
        $user  = "USERNAME"; /* USERNAME */
}
if (isset($_GET['count'])) {
    $user = $_GET['count'];
} else {
    $count = 20;
}

$getfield = "?screen_name=$user&count=$count";
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter  ->setGetfield($getfield)
                                ->buildOauth($url, $requestMethod)
                                ->performRequest(),$assoc = TRUE);

if($string["errors"][0]["message"] != "") {
    echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>".$string[errors][0]["message"]."</em></p>";
    exit();
}
foreach($string as $items){

        echo "Time and Date of Tweet: ".$items['created_at']."<br />";
        echo "Tweet: ". $items['text']."<br />";
        echo "Tweeted by: ". $items['user']['name']."<br />";
        echo "Screen name: ". $items['user']['screen_name']."<br />";
        echo "Followers: ". $items['user']['followers_count']."<br />";
        echo "Friends: ". $items['user']['friends_count']."<br />";
        echo "Listed: ". $items['user']['listed_count']."<br /><hr />";
    }
?>

Upvotes: 0

Rob K
Rob K

Reputation: 224

Take a look at: https://github.com/abraham/twitteroauth

With this library it is as simple as:

$twitterConnection = new TwitterOAuth(
    'COMSUMER KEY', // Consumer Key
    'CONSUMER SECRET',     // Consumer secret
    'ACCESS TOKEN',       // Access token
    'ACCESS TOKEN SECRET'      // Access token secret
);

$twitterData = $twitterConnection->get(
    'statuses/user_timeline',
    array(
        'screen_name'     => 'USERNAME',
        'count' => 3
    )
);

This returns an array of tweets similar to the V1.0 API.

You can create your app and get the required credentials here: https://dev.twitter.com/apps

Upvotes: 1

Related Questions