Thursday, December 18, 2014

Read Twitter with PHP on your website without using the API

I looking for a way to read Twitter with PHP for an application, I found that many source codes in PHP do not work because they are for the old version of Twitter, other PHP source codes are for the modern version but force you to sign up, get you a password identification, etc, and it was like overkill.
So I kept searching and found that there are other more simple methods, even too simple, so I researched a bit and here sack the work of my research on how to get data from Twitter with PHP, if someone finds it useful.
First I show you step by step code and the last item leave a link to download all the full code that captures messages (and other data) tweet with PHP.
Beware of making too many requests to Twitter with these routines, if you make more than 120 requests in less than 15 minutes, will ban you and stop working. To prevent this you can make a timer, so as to record the data and not reconnect to Twitter until after 1 minute (thus would be 60 requests per hour maximum). Later I will discuss in another article on how to implement a timer of this type with PHP.

Disable caching with PHP

For starters, it is highly recommended for this type of program disable all types of caches, since in some misconfigured and especially in mobile browsers, when the page is reloaded, the saved web page is displayed on the hard disk cache (or equivalent) and does not display the web server. And so, the new publications would not always the same would.
With the following code in PHP, we force a page in PHP are always loaded from the server and not the cache:

Converting text "links Twitter"

The following function is not mine, Walter public user reviews in web Css-tricks.com , and its mission is to process text and convert certain strings to Twitter links. For example if you are "I like the modern #tecnologia" becomes the word "#tecnologia" on a link to clicarlo, visualize recent tweets related to #tecnlogia.
$ Ret);
     $ Ret = preg_replace ("# (^ | [\ n]) ((www. | ftp) \ [^ \" \ t \ n \ r <] *) # " "\\ 1 \\ 2 "
$ Ret);
     $ Ret = preg_replace ("/ @ (\ w +) /" " @ \\ 1 "
$ Ret);
     $ Ret = preg_replace ("/ # (\ w +) /" " # \\ 1 "
$ Ret);
     return $ Ret;
}

Read Twitter with PHP and get information and messages from a user

Now we turn to capture messages from a user (a user only, not other things), which allow you to read Twitter with PHP and get your data into your own site.
At the beginning we have the variable "$ user" which will assign you a Twitter user name, in this case the user David Bravo which'm a fan (though he does not know me). And in the variable "$ numDeTweets" assign how many messages we see, in our case we put a 5 because we want to visualize their last 5 posts:
As you can see from the above example, at the end there are a number of objects that have left as comments. Theoretically should work according to the official documentation so far and according to other websites that talk about it, but in my tests these objects always return the value blank. If anyone is interested, here I leave.

Read posts anything on Twitter, using your browser

Now do something similar to the above but with a different method. Instead of reading or capture a user's tweets, we will extract data from a search, either a word or hashtag or anything else that has been published in Twitter.
The problem is that you can only recover a limited number of messages (only the last 15 tweets).
$ Subject = "#tecnologia";
$ Twitter = "Http://search.twitter.com/search.json?q=$tema";
$ ManejadorCurl = curl_init ();   
curl_setopt ($ manejadorCurl, CURLOPT_URL, $ Twitter);   
curl_setopt ($ manejadorCurl, CURLOPT_RETURNTRANSFER, 1);   
$ Response = curl_exec ($ manejadorCurl);
curl_close ($ manejadorCurl);   
$ Json = json_decode ($ response);   
foreach ($ Json -> results as $ Data)   
{   
     $ Text = "
Time: </ b>"
date ("Y / m / d H: i: s", strtotime ($ data -> created_at)).
// Extract date
     $ Text = . $ Text "
profile_image_url: "($
data -.> Profile_image_url)" \ ">";.
     $ Text = . $ Text "
Message:"
twitterify ($ data -> text).
// Extracts text
     $ Text = $ Text "
Id:"
($ data -> id);..
     $ Text = $ Text "
source:"
($ data -> source);..
     // The "geo" object is hard to read sometimes fails type "Catchable fatal error:
     Object of class stdClass // Could not be converted to string in ... "
     // $ Text = $ text "
geo" ($ data-> geo);..
/ *
// Do not work
$ Text = $ text "
retweeted" ($ data-> retweeted);..
. $ Text = $ text "
retweet_count" ($ data-> retweet_count).
$ Text = $ text "
in_reply_to_screen_name" ($ data-> in_reply_to_screen_name);..
$ Text = $ text "
in_reply_to_user_id" ($ data-> in_reply_to_user_id);..
$ Text = $ text "
in_reply_to_status_id" ($ data-> in_reply_to_user_id);..
$ Text = $ text "
favorited" ($ data-> favorited);..
$ Text = $ text "
truncated" ($ data-> truncated);..
$ Text = $ text "
retweet" ($ data-> retweet);..
$ Text = $ text "
user" ($ data-> user);..
. $ Text = $ text "
place" ($ data-> place).
$ Text = $ text "
coordinates" ($ data-> coordinates);..
$ Text = $ text "
contributors" ($ data-> contributors);..
$ Text = $ text "
screen_name" ($ data-> screen_name);..
$ Text = $ text "
name:" ($ data-> name);..
$ Text = $ text "
followers_count" ($ data-> followers_count);..
$ Text = $ text "
mentions:" ($ data-> mentions);..
$ Text = $ text "
links:" ($ data-> links);..
$ Text = $ text "
domains" ($ data-> domains);..
* /     
     echo $ Text "
-------------------.";
}   
As before, some labels return nothing (empty string) but here I leave if you need to something.

The "geo" object is a mystery to me, in some tweets have found containing "something" because I causes an error in the server type "Object of class stdClass Could not be converted to string in ...", but I can not extract information.
I tried different methods, treat as an array, etc and I have not found a way to display the contents of the object called "geo". On some websites suggest that might be the coordinates from which the message was sent, but no one explains how to read with a method that works. Did you know you by any chance?

Download the full source code

And finally, I leave here in a file all the full code to read tweets with PHP, running and tested, so you save time:
capturaTwitter.php
Please, if you are helpful or perfecting these routines, or discover faults, or find out more about more objects than can be extracted, post a comment. Thank you.

No comments:

Post a Comment