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.
With the following code in PHP, we force a page in PHP are always loaded from the server and not the cache:
$ Ret);
$ Ret);
$ Ret);
$ Ret); 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.
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.
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:
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
Time: </ b>" date ("Y / m / d H: i: s", strtotime ($ data -> created_at)). // Extract date
$ Text = . $ Text "
profile_image_url: "($ data -.> Profile_image_url)" \ ">";.
profile_image_url: "($ data -.> Profile_image_url)" \ ">";.
$ Text = . $ Text "
Message:" twitterify ($ data -> text). // Extracts text
Message:" twitterify ($ data -> text). // Extracts text
$ Text = $ Text "
Id:" ($ data -> id);..
Id:" ($ data -> id);..
$ Text = $ Text "
source:" ($ data -> source);..
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);..
geo" ($ data-> geo);..
/ *
// Do not work
$ Text = $ text "
retweeted" ($ data-> retweeted);..
retweeted" ($ data-> retweeted);..
. $ Text = $ text "
retweet_count" ($ data-> retweet_count).
retweet_count" ($ data-> retweet_count).
$ Text = $ text "
in_reply_to_screen_name" ($ data-> in_reply_to_screen_name);..
in_reply_to_screen_name" ($ data-> in_reply_to_screen_name);..
$ Text = $ text "
in_reply_to_user_id" ($ data-> in_reply_to_user_id);..
in_reply_to_user_id" ($ data-> in_reply_to_user_id);..
$ Text = $ text "
in_reply_to_status_id" ($ data-> in_reply_to_user_id);..
in_reply_to_status_id" ($ data-> in_reply_to_user_id);..
$ Text = $ text "
favorited" ($ data-> favorited);..
favorited" ($ data-> favorited);..
$ Text = $ text "
truncated" ($ data-> truncated);..
truncated" ($ data-> truncated);..
$ Text = $ text "
retweet" ($ data-> retweet);..
retweet" ($ data-> retweet);..
$ Text = $ text "
user" ($ data-> user);..
user" ($ data-> user);..
. $ Text = $ text "
place" ($ data-> place).
place" ($ data-> place).
$ Text = $ text "
coordinates" ($ data-> coordinates);..
coordinates" ($ data-> coordinates);..
$ Text = $ text "
contributors" ($ data-> contributors);..
contributors" ($ data-> contributors);..
$ Text = $ text "
screen_name" ($ data-> screen_name);..
screen_name" ($ data-> screen_name);..
$ Text = $ text "
name:" ($ data-> name);..
name:" ($ data-> name);..
$ Text = $ text "
followers_count" ($ data-> followers_count);..
followers_count" ($ data-> followers_count);..
$ Text = $ text "
mentions:" ($ data-> mentions);..
mentions:" ($ data-> mentions);..
$ Text = $ text "
links:" ($ data-> links);..
links:" ($ data-> links);..
$ Text = $ text "
domains" ($ data-> domains);..
domains" ($ data-> domains);..
* /
echo $ Text "
-------------------.";
-------------------.";
}
As before, some labels return nothing (empty string) but here I leave if you need to something. 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