Hey everyone! I’m trying to get recent tweets from a user’s timeline using PHP and the new Twitter API 1.1. My old code doesn’t work anymore since they retired the 1.0 version. I’ve looked at the official docs but I’m getting an authentication error. I’ve tried a few libraries but they’re either not working or way too complex for what I need.
Here’s a simple example of what I was using before:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=johndoe&count=5');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$tweets = json_decode($result, true);
foreach ($tweets as $tweet) {
echo $tweet['text'] . '<br>';
}
Can anyone show me the easiest way to do this with the new API? I just want to grab the latest tweets without any fancy stuff. Thanks!
yo, i had the same issue. try using abraham’s twitteroauth library. its pretty easy:
require_once('twitteroauth/autoload.php');
use Abraham\TwitterOAuth\TwitterOAuth;
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
$tweets = $connection->get('statuses/user_timeline', ['screen_name' => 'johndoe', 'count' => 5]);
foreach ($tweets as $tweet) {
echo $tweet->text . '<br>';
}
jus replace the caps with ur api keys. works like a charm for me
I’ve been through this exact situation recently when Twitter updated their API. Here’s what worked for me:
First, you’ll need to set up a developer account and create an app to get your API keys. Once you have those, you can use the following code:
require_once('TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => "YOUR_ACCESS_TOKEN",
'oauth_access_token_secret' => "YOUR_ACCESS_TOKEN_SECRET",
'consumer_key' => "YOUR_CONSUMER_KEY",
'consumer_secret' => "YOUR_CONSUMER_SECRET"
);
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=johndoe&count=5';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$tweets = json_decode($response, true);
foreach($tweets as $tweet) {
echo $tweet['text'] . '<br>';
}
This uses the TwitterAPIExchange class, which you can find on GitHub. It’s pretty straightforward and handles the OAuth stuff for you. Just remember to replace the placeholder API keys with your own.
liamj
May 17, 2025, 5:27am
4
Having worked with the Twitter API recently, I can confirm that OAuth authentication is now required. Here’s a streamlined approach using cURL:
function getRecentTweets($username, $count = 5) {
$token = 'YOUR_BEARER_TOKEN';
$url = 'https://api.twitter.com/2/tweets/search/recent?query=from:' . $username . '&max_results=' . $count;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $token]);
$response = curl_exec($ch);
curl_close($ch);
$tweets = json_decode($response, true);
return $tweets['data'] ?? [];
}
$recentTweets = getRecentTweets('johndoe');
foreach ($recentTweets as $tweet) {
echo $tweet['text'] . '<br>';
}
Remember to replace ‘YOUR_BEARER_TOKEN’ with your actual bearer token from the Twitter Developer Portal. This method uses the v2 API, which is more current and efficient for basic tweet retrieval.