Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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) …
<?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
@planetoftheweb

This comment has been minimized.

Show comment Hide comment
@planetoftheweb

planetoftheweb Jul 2, 2013

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.

Owner

planetoftheweb commented Jul 2, 2013

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.

@planetoftheweb

This comment has been minimized.

Show comment Hide comment
@planetoftheweb

planetoftheweb Jul 3, 2013

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

Owner

planetoftheweb commented Jul 3, 2013

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

@dldpidodido

This comment has been minimized.

Show comment Hide comment
@dldpidodido

dldpidodido Sep 10, 2013

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

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

@matharchod

This comment has been minimized.

Show comment Hide comment
@matharchod

matharchod Nov 11, 2013

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 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.

@digitaldreamsol

This comment has been minimized.

Show comment Hide comment
@digitaldreamsol

digitaldreamsol Nov 20, 2013

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 );

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 );

@vikitripathi

This comment has been minimized.

Show comment Hide comment
@vikitripathi

vikitripathi Jun 17, 2014

hey show the index.php file too
how to convert that data coming on browser from my tweet feed to html and stylize my way

hey show the index.php file too
how to convert that data coming on browser from my tweet feed to html and stylize my way

@hsikandar

This comment has been minimized.

Show comment Hide comment
@hsikandar

hsikandar Feb 16, 2015

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

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

@raihan004

This comment has been minimized.

Show comment Hide comment
@raihan004

raihan004 Apr 20, 2015

thanks :D

thanks :D

@RobMcCoy66

This comment has been minimized.

Show comment Hide comment
@RobMcCoy66

RobMcCoy66 May 4, 2015

Is there a way to use this to pull tweets from a list, rather than from a user? I thought it would be as simple as changing the URL and adding the necessary parameters, but that just throws a 400 error.

Is there a way to use this to pull tweets from a list, rather than from a user? I thought it would be as simple as changing the URL and adding the necessary parameters, but that just throws a 400 error.

@SHUBH4765

This comment has been minimized.

Show comment Hide comment
@SHUBH4765

SHUBH4765 Jun 10, 2015

found error400 while running tweets_json.php" from my server. how to remove it?

found error400 while running tweets_json.php" from my server. how to remove it?

@nantaphong

This comment has been minimized.

Show comment Hide comment
@nantaphong

nantaphong Oct 2, 2015

Thanks for you

Thanks for you

@bchibb

This comment has been minimized.

Show comment Hide comment
@bchibb

bchibb Feb 12, 2016

Great tutorial! I almost have it working. The only thing I think I am missing is the definition of the "callback=listTweets". Can anyone provide the code for "listTweets"? In the youtube video, (https://www.youtube.com/watch?v=GQaPt-gQVRI), around 6:25, the author mentions he programmed listTweets in his "php document". I assume he is referring to the "tweets_json.php" but I'm not sure. BTW I am new to the API functionality.

bchibb commented Feb 12, 2016

Great tutorial! I almost have it working. The only thing I think I am missing is the definition of the "callback=listTweets". Can anyone provide the code for "listTweets"? In the youtube video, (https://www.youtube.com/watch?v=GQaPt-gQVRI), around 6:25, the author mentions he programmed listTweets in his "php document". I assume he is referring to the "tweets_json.php" but I'm not sure. BTW I am new to the API functionality.

@howdyhyber

This comment has been minimized.

Show comment Hide comment
@howdyhyber

howdyhyber Oct 1, 2016

I am viewing the video in youtube you have made, and I followed all your instruction but I had encountered these problem:

Notice: Undefined index: count in C:\xampp\htdocs\Maps\tweets_json.php on line 17

Notice: Undefined index: screen_name in C:\xampp\htdocs\Maps\tweets_json.php on line 21

Notice: Undefined index: twitter_path in C:\xampp\htdocs\Maps\tweets_json.php on line 25

Notice: Undefined index: callback in C:\xampp\htdocs\Maps\tweets_json.php on line 34
[]

How to solve this?

I am viewing the video in youtube you have made, and I followed all your instruction but I had encountered these problem:

Notice: Undefined index: count in C:\xampp\htdocs\Maps\tweets_json.php on line 17

Notice: Undefined index: screen_name in C:\xampp\htdocs\Maps\tweets_json.php on line 21

Notice: Undefined index: twitter_path in C:\xampp\htdocs\Maps\tweets_json.php on line 25

Notice: Undefined index: callback in C:\xampp\htdocs\Maps\tweets_json.php on line 34
[]

How to solve this?

@sohailriaz

This comment has been minimized.

Show comment Hide comment
@sohailriaz

sohailriaz Apr 4, 2017

Any one help i got this error in browser
Notice: Undefined index: count in C:\xampp\htdocs\twitter\tweets_json.php on line 17

Notice: Undefined index: screen_name in C:\xampp\htdocs\twitter\tweets_json.php on line 21

Notice: Undefined index: twitter_path in C:\xampp\htdocs\twitter\tweets_json.php on line 25
Error ID: 0
Error: error setting certificate verify locations: CAfile: C:\xampp\htdocs\twitter\cacert.pem CApath: C:\xampp\htdocs\twitter

Any one help i got this error in browser
Notice: Undefined index: count in C:\xampp\htdocs\twitter\tweets_json.php on line 17

Notice: Undefined index: screen_name in C:\xampp\htdocs\twitter\tweets_json.php on line 21

Notice: Undefined index: twitter_path in C:\xampp\htdocs\twitter\tweets_json.php on line 25
Error ID: 0
Error: error setting certificate verify locations: CAfile: C:\xampp\htdocs\twitter\cacert.pem CApath: C:\xampp\htdocs\twitter

@kidalikevin

This comment has been minimized.

Show comment Hide comment
@kidalikevin

kidalikevin Apr 28, 2017

Is it possible to get tweet fav(likes) and retweets count.

Is it possible to get tweet fav(likes) and retweets count.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment