Now twitter is playing a major roll to our day. Many of us is using twitter. Now lets play something with it. Today i will use JSON to play with twitter LOL.
Lets play with our statuses. If you are using twitter, then your status url is http://twitter.com/status/user_timeline/YOUR_USERNAME.json. So if your twitter id is tareq_cse then the url will be http://twitter.com/status/user_timeline/tareq_cse.json. Now i will add a extra parameter here and this is count. This url shows the last 20 status update of yours. So if i need only 5 update then? Then we will add a count variable there. So the url will be http://twitter.com/status/user_timeline/tareq_cse.json?count=5
Now come to the php code.
[php]
<?php
$json = file_get_contents("http://twitter.com/status/user_timeline/tareq_cse.json?count=10", true); //getting the file content
$decode = json_decode($json, true); //getting the file content as array
echo "<pre>";
print_r($decode);
echo "</pre>";
?>
[/php]
Now run the code and see. You will get output like this
[html]
Array
(
[0] => Array
(
[text] => is digging wordpress
[in_reply_to_user_id] =>
[user] => Array
(
[notifications] =>
[time_zone] => Dhaka
[description] => A blogger, microblogger and learner
[following] =>
[utc_offset] => 21600
……………
……………
[profile_text_color] => 666666
[location] => Rajshahi, Bangladesh
[id] => 15842888
[profile_background_image_url] => http://static.twitter.com/images/themes/theme9/bg.gif
[profile_link_color] => 2FC2EF
)
[favorited] =>
[in_reply_to_screen_name] =>
[truncated] =>
[created_at] => Sun May 03 20:56:00 +0000 2009
[id] => 1689697302
[in_reply_to_status_id] =>
[source] => TwitterFox
)
)
[/html]
Now get some usefull data from this. If we need the last 10 twitter update write the code
[php]
<?php
$json = file_get_contents("http://twitter.com/status/user_timeline/tareq_cse.json?count=10", true);
$decode = json_decode($json, true);
echo "<pre>";
$count = count($decode); //counting the number of status
for($i=0;$i<$count;$i++){
echo $decode[$i][text]."<br>";
}
echo "</pre>";
?>
[/php]
Lets pull some userdata
[php]
<?php
$json = file_get_contents("http://twitter.com/status/user_timeline/tareq_cse.json?count=10", true);
$decode = json_decode($json, true);
echo "<img src=\"".$decode[0][user][profile_image_url]."\"
"; //getting the profile image
echo "Name: ".$decode[0][user][name]."<br>"; //getting the username
echo "Web: ".$decode[0][user][url]."
"; //getting the web site address
echo "Location: ".$decode[0][user][location]."<br>"; //user location
echo "Updates: ".$decode[0][user][statuses_count]."<br>"; //number of updates
echo "Follower: ".$decode[0][user][followers_count]."<br>"; //number of followers
echo "Following: ".$decode[0][user][friends_count]."<br>"; // following
echo "Description: ".$decode[0][user][description]."<br>"; //user description
?>
[/php]
Following this method, you can pull any data by analyzing the first output
Nice writeup… carrry on 🙂
Thanks Lenin Vai 🙂
very nice bit of code
how do you get it to have working hyper links from the tweet when it displays on your page?
any help on this would be great
You may use regular expressions to find out any link and replace them with link tag
Im not that great at coding more of a designer can you spoon feed that to me?
would be great if you could
Perfect. This is exactly what I needed for my project. Works awesome!
Great tutorial, simple but powerful.
However, what if twitter’s api fails? Have you got a fall back to stop your whole website failing due to the twitter drop out?
I think by making the twitter json parser script a function then running it in an if script would work?
like so:
function read_twit() {
$json = file_get_contents("http://twitter.com/status/user_timeline/trafficscotland.json?count=5", true);
$decode = json_decode($json, true);
$count = count($decode); //counting the number of status
for($i=0;$i<$count;$i++){
echo "".$decode[$i][text][/text]."";
}
And then calling it into your website like so:
Any thoughts?
Will not this code fail? 😕
after file_get_contents,
if($json === FALSE) return;
this will help i think 🙂
indeed 😀
This code may not be working in live server. If your page permission is not set to “777” then you can’t directly access the page using “file_get_contents” on the fly.
file_get_contents is a pretty easy way to read that file. I was definitely over-thinking it 🙂
Thanks for this.
Nice tutorial, easy to understand the codes.. :).. keep up the good work dude.. cheers!!
which is the error?????????
which is the error?????????
$json = file_get_contents(“https://api.twitter.com/1/trends/1.json”);
$json = json_decode($json, true);
echo $json[0][‘name’];
quotes surrounding name?
Very helpful as addition to the PHP Json_decode docs.
Saved me a good deal to time. Thanks!
Simple and straight to the point. Took me 5 mins to adapt for my own needs, thanks a lot for sharing! You can see the results at http://timowallenius.com in few minutes once I get my dashboard finalized. I’ll combine it with google analytics, WP post count and project completion tracker.
Thanks a lot. helped me a lot
I’ve been trying this with
file_get_contents(“https://stream.twitter.com/1/statuses/filter.json?locations=35.43,79.10,35.54,79.3”)
but file_get_contents doesn’t appear to be returning anything. Not even a “false.”
Any idea what might be wrong?
did you actually tried to visit the url with the browser? It needs authentication to retrieve the data and fails.
It’s Possible for to get Email Id
Nope, twitter doesn’t give email address.
Great code, very easy to implement in my project. I am having an issue though with using multiple keywords. It will only stream the first keyword. I’ve attempted using “keyword1, keyword2” also “keyword1 OR keyword2”, neither has worked for me. Is there anything else I am missing, I looked through all the developer documentation as well as many websites and cannot find an example of submitting multiple keywords, although it clearly states you can submit 200 keywords at a time. I am using the same exact code as you have above only modifying the output to my webpage. Any help would be greatly appreciated.
Jose.
Whoops, sorry, I mistakenly posted that I was using the same code, actually it’s modified for the streaming api. Here is the code I am using:
$keyword = $_POST[“keyword”];
set_time_limit(0);
$query_data = array(‘track’ => ‘apple, lady gaga’);
$user = ‘username’; // replace with your account
$pass = ‘password’; // replace with your account
$fp = fsockopen(“ssl://stream.twitter.com”, 443, $errno, $errstr, 30);
if(!$fp){
print “$errstr ($errno)\n”;
} else {
$request = “GET /1/statuses/filter.json?” . http_build_query($query_data) . ” HTTP/1.1\r\n”;
//$request = “GET /1/account/rate_limit_status.json?” . http_build_query($query_data) . ” HTTP/1.1\r\n”;
$request .= “Host: stream.twitter.com\r\n”;
$request .= “Authorization: Basic ” . base64_encode($user . ‘:’ . $pass) . “\r\n\r\n”;
fwrite($fp, $request);
$i=0;
//echo “Keywords: $keyword “;
while(!feof($fp)){
$json = fgets($fp);
$data = json_decode($json, true);
if($data){
//
// Do something with the data!
//
echo “Tweet: “;
print_r($data);
echo “”; //getting all the tweets
$i++;
if ($i>100) {
break;
}
}
}
fclose($fp);
}
?>
Did you saw the twitter search api? Your code seems complicated to me :S
Hi, what if i want to get all the tweets from my own contacts?
@Infobot2012 I am not sure if you can get all the tweets from twitter (maybe last 3200 tweets), there isn’t a easy way around. You can search for it.
Getting error message “Warning: file_get_contents() [function.file-get-contents]: Unable to find the wrapper “https” – did you forget to enable it when you configured PHP” in relation to line “$direct_message = $OAuth = file_get_contents(‘https://api.twitter.com/1/direct_messages.json?count=1&page=’, true);”
[code]
$OAuth = new TwitterOAuth($consumer_key,$consumer_secret, $oAuthToken, $oAuthSecret);
$direct_message = $OAuth = file_get_contents(‘https://api.twitter.com/1/direct_messages.json?count=1&page=’, true);
$decode = json_decode($direct_message, true); // this gets the json file content as an array
[/code]
@Infobot2012 The solution is in the error message. Your host doesn’t support https protocol
Hi,
Thanks so much for posting this. I’m finding it to be very useful for a twitter-based project that I’m working on right now. I was able to get this working a couple months ago. However, I’ve recently revisited it and am having problems. The JSON link doesn’t seem to be working properly anymore.
http://twitter.com/status/user_timeline/tareq_cse.json?count=5
I get
message: “Sorry, that page does not exist”,
code: 34
Any idea what’s going on? I’m at a loss.
Hey, after some more research I’ve actually found the solution. The link below will work, hopefully this will help other people with the same problem.
https://api.twitter.com/1/statuses/user_timeline/tareq_cse.json?count=5
Not working at all
Warning: file_get_contents(http://twitter.com/status/user_timeline/GurubakshGulati.json?count=10) [function.file-get-contents]: failed to open stream: Unable to find the socket transport “ssl” – did you forget to enable it when you configured PHP? in D:\xampp\htdocs\projects\twitter\2.php on line 2
Notice: Use of undefined constant user – assumed ‘user’ in D:\xampp\htdocs\projects\twitter\2.php on line 5
Notice: Use of undefined constant profile_image_url – assumed ‘profile_image_url’ in D:\xampp\htdocs\projects\twitter\2.php on line 5
Notice: Use of undefined constant user – assumed ‘user’ in D:\xampp\htdocs\projects\twitter\2.php on line 7
Notice: Use of undefined constant name – assumed ‘name’ in D:\xampp\htdocs\projects\twitter\2.php on line 7
Name:
Notice: Use of undefined constant user – assumed ‘user’ in D:\xampp\htdocs\projects\twitter\2.php on line 8
Notice: Use of undefined constant url – assumed ‘url’ in D:\xampp\htdocs\projects\twitter\2.php on line 8
Web:
Notice: Use of undefined constant user – assumed ‘user’ in D:\xampp\htdocs\projects\twitter\2.php on line 10
Notice: Use of undefined constant location – assumed ‘location’ in D:\xampp\htdocs\projects\twitter\2.php on line 10
Location:
Notice: Use of undefined constant user – assumed ‘user’ in D:\xampp\htdocs\projects\twitter\2.php on line 11
Notice: Use of undefined constant statuses_count – assumed ‘statuses_count’ in D:\xampp\htdocs\projects\twitter\2.php on line 11
Updates:
Notice: Use of undefined constant user – assumed ‘user’ in D:\xampp\htdocs\projects\twitter\2.php on line 12
Notice: Use of undefined constant followers_count – assumed ‘followers_count’ in D:\xampp\htdocs\projects\twitter\2.php on line 12
Follower:
Notice: Use of undefined constant user – assumed ‘user’ in D:\xampp\htdocs\projects\twitter\2.php on line 13
Notice: Use of undefined constant friends_count – assumed ‘friends_count’ in D:\xampp\htdocs\projects\twitter\2.php on line 13
Following:
Notice: Use of undefined constant user – assumed ‘user’ in D:\xampp\htdocs\projects\twitter\2.php on line 14
Notice: Use of undefined constant description – assumed ‘description’ in D:\xampp\htdocs\projects\twitter\2.php on line 14
Description:
Thanks for sharing this it’s very interesting.