Javier Villanueva
Javier Villanueva

Reputation: 4048

Can't get latest tweets using Twitter API 1.1 and PHP

I'm trying to get the latest tweets from a particular user using PHP and CURL and the new 1.1 API, I created my new App in the twitter website and went to the OAuth tool tab to generate the signature and according to the page it generates the following curl command:

curl --get 'https://api.twitter.com/1.1/statuses/user_timeline.json?count=2&screen_name=twitter' --header 'Authorization: OAuth oauth_consumer_key="Waqd223QDuge6l9UboBldg", oauth_nonce="8a9beae863838b0ba2bc6ac03bc9757e", oauth_signature="u2PrZ5g14HGb39pHoYl9azHp6vg%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1355408402", oauth_token="171967067-lyLujNed9QyVBfmlfAkFXiUt5KIQkqrCMwg6utFc", oauth_version="1.0"'

Which works fine in my terminal, however when I try to replicate it in PHP I get a "Could not authenticate you" error, this is what I have so far:

$options = array(
    URLOPT_HTTPHEADER      => array('Authorization: OAuth oauth_consumer_key="Waqd223QDuge6l9UboBldg", oauth_nonce="8a9beae863838b0ba2bc6ac03bc9757e", oauth_signature="toLnir5cHUUvEj8X29SdzjlOTXc%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="' . time() . '", oauth_token="171967067-lyLujNed9QyVBfmlfAkFXiUt5KIQkqrCMwg6utFc", oauth_version="1.0"'),
    CURLOPT_HEADER         => false,
    CURLOPT_URL            => 'https://api.twitter.com/1.1/statuses/user_timeline.json?count=2&screen_name=twitter',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false
);

$feed = curl_init();
curl_setopt_array($feed, $options);
$json = curl_exec($feed);
curl_close($feed);

$twitter_data = json_decode($json);

var_dump($twitter_data);

The only thing I'm changing is the oauth_timestamp where I use the time() function to get the current time, I figure I must be passing the wrong options to the curl function but I can't figure it out.

Thanks in advance.

Upvotes: 1

Views: 24910

Answers (3)

Albert Kozłowski
Albert Kozłowski

Reputation: 476

Really simple php wrapper for Twitter 1.1 rest api

https://github.com/vojant/Twitter-php

Upvotes: 0

Animal Rights
Animal Rights

Reputation: 9377

Took me a while to get something working but I finally did and have pushed a standalone class and demo to GitHub that should be pretty easy to follow.

https://github.com/skaterdav85/twitter-library

Upvotes: 0

lackovic10
lackovic10

Reputation: 1004

Here you can find an example of getting tweets with the twitter api 1.1 and curl

Simplest PHP example for retrieving user_timeline with Twitter API version 1.1

Upvotes: 3

Related Questions