public
Created

This gist will let you retrieve a series of tweets using the twitter v1.1 API. There's a few steps you have to do before you use this. 1. First, you need to go to http://dev.twitter.com/apps and create a new application. 2. You'll also need to download this library. https://github.com/themattharris/tmhOAuth (You only need the tmhOAuth.php file) 3. You might need to download this certificate (http://curl.haxx.se/ca/cacert.pem) and place it in the same folder as this php file. You can call this php file directly to get the most recent tweets from the user owning the app. You can also pass along some parameters - tweets_json.php?count=10 //get the last 10 tweets - tweets_json.php?count=10&screen_name=planetoftheweb //same as above from the planetoftheweb account (me) - tweets_json.php?count=10&screen_name=planetoftheweb&callback=listTweets //same as above with a jsonp wrapper (listTweets();) Watch the video on YouTube (https://www.youtube.com/watch?v=GQaPt-gQVRI)

  • Download Gist
tweets_json.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
<?php
require 'tmhOAuth.php'; // Get it from: https://github.com/themattharris/tmhOAuth
 
// Use the data from http://dev.twitter.com/apps to fill out this info
// notice the slight name difference in the last two items)
 
$connection = new tmhOAuth(array(
'consumer_key' => '',
'consumer_secret' => '',
'user_token' => '', //access token
'user_secret' => '' //access token secret
));
 
// set up parameters to pass
$parameters = array();
 
if ($_GET['count']) {
$parameters['count'] = strip_tags($_GET['count']);
}
 
if ($_GET['screen_name']) {
$parameters['screen_name'] = strip_tags($_GET['screen_name']);
}
 
if ($_GET['twitter_path']) { $twitter_path = $_GET['twitter_path']; } else {
$twitter_path = '1.1/statuses/user_timeline.json';
}
 
$http_code = $connection->request('GET', $connection->url($twitter_path), $parameters );
 
if ($http_code === 200) { // if everything's good
$response = strip_tags($connection->response['response']);
 
if ($_GET['callback']) { // if we ask for a jsonp callback function
echo $_GET['callback'],'(', $response,');';
} else {
echo $response;
}
} else {
echo "Error ID: ",$http_code, "<br>\n";
echo "Error: ",$connection->response['error'], "<br>\n";
}
 
// You may have to download and copy http://curl.haxx.se/ca/cacert.pem

I guess the gist description doesn't take markdown formatting, so here is the same thing, but a lot prettier.

This gist will let you retrieve a series of tweets using the twitter v1.1 API. There's a few steps you have to do before you use this.

  1. First, you need to go to http://dev.twitter.com/apps and create a new application.
  2. You'll also need to download this library. https://github.com/themattharris/tmhOAuth (You only need the tmhOAuth.php file)
  3. You might need to download this certificate (http://curl.haxx.se/ca/cacert.pem) and place it in the same folder as this php file.

You can call this php file directly to get the most recent tweets from the user owning the app.

You can also pass along some parameters

  • tweets_json.php?count=10 //get the last 10 tweets
  • tweets_json.php?count=10&screen_name=planetoftheweb //same as above from the planetoftheweb account (me)
  • tweets_json.php?count=10&screen_name=planetoftheweb&callback=listTweets //same as above with a jsonp wrapper (listTweets();)

Hope this is useful to someone, I'll do a video explaining things soon.

Oh yeah...the video tutorial is now live on YouTube (https://www.youtube.com/watch?v=GQaPt-gQVRI)

i got an error "Error ID: 401
Error: " when i type the directory of "tweets_json.php" from my server.

I love this one. I was looking for a simple script that:

  1. Returned a JSON feed of all of my tweets
  2. Was easy to set up
  3. Didn't require me to learn how to use the new Twitter API (yet)

I watched the YouTube video twice and followed the instructions. I was returning JSON from my Twitter feed in less than 15 minutes. Solid.

I have tried to get this working but am getting:
Fatal error: Call to a member function request() on a non-object in ******/index.php on line 29

$http_code = $connection->request('GET', $connection->url($twitter_path), $parameters );

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.