Need Twitter API clarified please

how exactly does Twitter API work?

I’m afraid it’s not too clear here,
https://dev.twitter.com/docs/using-search

is there a download? is the API a javascript file, is it back-end stuff? it uses REST API? what does this mean exactly?

I don’t get this documentation… the only code examples are what shows when you get an error… hilarious…

I need to write a little Twitter search thingie… I can’t make heads or tails of this… I can’t even find a “download” link anywhere…

I’m looking at this, https://twitter.com/search-home, but I still don’t see how they connect to the API…

thank you…

Hi maya90,

Sorry, I’d seen your post and had intended to come back and answer it, but other things came up and I ended up forgetting.

Twitter recently turned off v1 of their API, and using v1.1 requires you to be authenticated to do anything. Probably the easiest way to do this is to have a PHP script on your server to act as the middleman and do the authentication for you. Sitepoint recently posted an article showing how to do this for a simple twitter feed, which should get you pointed in the right direction, but feel free to post back here if you need additional help.

thank you very much for your response, fretburner… I will certainly read article you posted…

I need to write a little app to search Twitter posts…

figured I’d base it on this…
https://twitter.com/search-home
make sense?

(I can run PHP on my machine, so no problem on that front…:wink:

thank you…

fretburner,

thank you for posting this…

am trying to follow this… followed instructions to generate those tokens…

but this code is not printing anything…


require('twitteroauth.php'); 
$consumerkey = ''<got from that auth pg>';
$consumersecret = ''<got from that auth pg>';
$accesstoken = ''<got from that auth pg>'; 
$accesstokensecret = '<got from that auth pg>';  
$twitter = new TwitterOAuth($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets = $twitter->get('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=NewYorker&count=10');
print_r($tweets);

I used my own account, nothing prints… then switched to a more “public” account, like @NewYorker (according to this code you don’t need ‘@’ in front of it, yes?)

thank you…

I think you just need to leave out the first part of the URL, as the TwitterOAuth library adds that itself, so try changing this line:

$tweets = $twitter->get('statuses/user_timeline.json?screen_name=NewYorker&count=10');

hmmmm…

thank you freburner…

ok, I did what you said, it’s still not working… and the weird thing is, I added plain text to the printing stmt, just for testing, even that doesn’t print (and I don’t get errors…)

this is my code now:



$tweets = $twitter->get('statuses/user_timeline.json?screen_name=NewYorker&count=10');
echo "<p>tweets:</p>" + $tweets   //  does not print at all.... not even the stuff I hard-coded in there......

thank you…

Try doing

var_dump($tweets);

and see if you get anything.

hmmm… where do I put this, exactly? bef

thank you fretburner, I appreciate your help…

Put it after the call to the API:


$tweets = $twitter->get('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=NewYorker&count=10');
var_dump($tweets);

Basically it should give you some output, even if only to tell you that $tweets is empty.

ok… am getting all this stuff now…


array(10) { [0]=> object(stdClass)#5 (23) { ["created_at"]=> string(30) "Tue Aug 20 18:35:20 +0000 2013" ["id"]=> int(369890411156086784) ["id_str"]=> string(18) "369890411156086784" ["text"]=> string(144) "Thirty years ago today, Prince, Michael Jackson, & James Brown shared a stage. @bengreenman on a musical holy trinity http://t.co/vw5Ga1CArs" ["source"]=> string(63) "HootSuite" ["truncated"]=> bool(false) ["in_reply_to_status_id"]=> NULL ["in_reply_to_status_id_str"]=> NULL ["in_reply_to_user_id"]=> NULL ["in_reply_to_user_id_str"]=> NULL ["in_reply_to_screen_name"]=> NULL ["user"]=> object(stdClass)#6 (39) { ["id"]=> int(14677919) ["id_str"]=> string(8) "14677919" ["name"]=> string(14) "The New Yorker" ["screen_name"]=> string(9) "NewYorker" ["location"]=> string(12) "New York, NY" ["description"]=> string(151) "The New Yorker is a weekly magazine with a mix of reporting of politics and culture, humor and cartoons, fiction and poetry, and reviews and criticism." ["url"]=> string(22) "http://t.co/RJjEvb2dV6" ["entities"]=> object(stdClass)#7 (2) { ["url"]=> object(stdClass)#8 (1) { ["urls"]=> array(1) { [0]=> object(stdClass)#9 (4) { ["url"]=> string(22) "http://t.co/RJjEvb2dV6" ["expanded_url"]=> string(24) "http://www.newyorker.com" ["display_url"]=> string(13) "newyorker.com" ["indices"]=> array(2) { [0]=> int(0) [1]=> int(22) } } } } ["description"]=> object(stdClass)#10 (1) { ["urls"]=> array(0) { } } } ["protected"]=> bool(false) ["followers_count"]=> int(2509590) ["friends_count"]=> int(251) ["listed_count"]=> int(42918) ["created_at"]=> string(30) "Tue May 06 19:36:33 +0000 2008" ["favourites_count"]=> int(7) ["utc_offset"]=> int(-14400) ["time_zone"]=> string(26) "Eastern Time (US & Canada)" ["geo_enabled"]=> bool(false) ["verified"]=> bool(true) ["statuses_count"]=> int(13343) ["lang"]=> string(2) "en" ["contributors_enabled"]=> bool(false) ["is_translator"]=> bool(false) ["profile_background_color"]=> string(6) "9AE4E8" ["profile_background_image_url"]=> string(81) "http://a0.twimg.com/profile_background_images/55790865/Twitter_BG_Final_final.jpg" ["profile_background_image_url_https"]=> string(83) "https://si0.twimg.com/profile_background_images/55790865/Twitter_BG_Final_final.jpg" ["profile_background_tile"]=> bool(false) ["profile_image_url"]=> string(73) "http://a0.twimg.com/profile_images/553327850/Eustacewbutterfly_normal.png" ["profile_image_url_https"]=> string(75) "https://si0.twimg.com/profile_images/553327850/Eustacewbutterfly_normal.png" ["profile_banner_url"]=> string(57) "https://pbs.twimg.com/profile_banners/14677919/1355412486" ["profile_link_color"]=> string(6) "0084B4" ["profile_sidebar_border_color"]=> string(6) "BDDCAD" ["profile_sidebar_fill_color"]=> string(6) "DDFFCC" ["profile_text_color"]=> string(6) "333333" ["profile_use_background_image"]=> bool(true) ["default_profile"]=> bool(false) ["default_profile_image"]=> bool(false) ["following"]=> NULL ["follow_request_sent"]=> bool(false) ["notifications"]=> NULL } ["geo"]=> NULL ["coordinates"]=> NULL ["place"]=> NULL ["contributors"]=> NULL ["retweet_count"]=> int(106) ["favorite_count"]=> int(30) ["entities"]=> object(stdClass)#11 (4) { ["hashtags"]=> array(0) { } ["symbols"]=> array(0) { } ["urls"]=> array(1) { [0]=> object(stdClass)#12 (4) { ["url"]=> string(22) "http://t.co/vw5Ga1CArs" ["expanded_url"]=> string(21) "http://nyr.kr/167pzhL" ["display_url"]=> string(14) "nyr.kr/167pzhL" ["indices"]=> array(2) { [0]=> int(122) [1]=> int(144) } } } ["user_mentions"]=> array(1) { [0]=> object(stdClass)#13 (5) { ["screen_name"]=> string(11) "bengreenman" ["name"]=> string(12) "Ben Greenman" ["id"]=> int(113490650) ["id_str"]=> string(9) "113490650" ["indices"]=> array(2) { [0]=> int(83) [1]=> int(95) } } } } ["favorited"]=> bool(false) ["retweeted"]=> bool(false) ["possibly_sensitive"]=> bool(false) ["lang"]=> string(2) "en" } [1]=> object(stdClass)#14 (23) { ["created_at"]=> string(30) "Tue Aug 20 17:32:21 +0000 2013" ["id"]=> int(369874560273088513) 

etc etc etc…

is the “real” thing I should be getting?? just in “raw” format???

meaning I’m getting somewhere, right? :wink: what I need to do is actually write a Twitter search thingie (search for terms occurring in tweets…)

thank you very much for your help…:slight_smile:

OK, so what you’ve got now is an array of tweets as PHP StdClass objects… you could do some further processing in PHP if you wanted, before you output the results.

Alternatively, you can tell the TwitterOAuth library not to decode the results, and it’ll just be returned to you as JSON, which you could output back to your JS and do your formatting there:


$twitter = new TwitterOAuth($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$twitter->decode_json = false;

$tweets = $twitter->get('https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=NewYorker&count=10');

header('Content-type: application/json');
echo $tweets;

so this is essentially “raw” data like from a public feed or something?

I’m having a hard parsing this array (I mean “manually” parsing… just figuring out where each element ends…)
(it looks a little like an object, or a two-dimensional array (key-val pairs…))

so I guess the next step is parse according to taste…:wink:
(I love how you can get it returned as JSON…:wink:

PS: do you know how I would run a twitter-search thingie with this?

I need to write a little app that searches for tweets containing user-entered search terms…

thank you very much, fretburner…

so this what I get for just one tweet… (if do count “1” on this line,

$tweets = $twitter->get('statuses/user_timeline.json?screen_name=NewYorker&count=1');  

)

string(2754) "[{"created_at":"Tue Aug 20 21:32:42 +0000 2013","id":369935043478966272,"id_str":"369935043478966272","text":"\\"I had an autoimmune disorder, but now it seemed to have me.\\u201d @meghanor on living with autoimmune disease (sub req): http:\\/\\/t.co\\/JMOeHGdglm","source":"\\u003ca href=\\"http:\\/\\/www.hootsuite.com\\" rel=\\"nofollow\\"\\u003eHootSuite\\u003c\\/a\\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":14677919,"id_str":"14677919","name":"The New Yorker","screen_name":"NewYorker","location":"New York, NY","description":"The New Yorker is a weekly magazine with a mix of reporting of politics and culture, humor and cartoons, fiction and poetry, and reviews and criticism.","url":"http:\\/\\/t.co\\/RJjEvb2dV6","entities":{"url":{"urls":[{"url":"http:\\/\\/t.co\\/RJjEvb2dV6","expanded_url":"http:\\/\\/www.newyorker.com","display_url":"newyorker.com","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":2509975,"friends_count":251,"listed_count":42916,"created_at":"Tue May 06 19:36:33 +0000 2008","favourites_count":7,"utc_offset":-14400,"time_zone":"Eastern Time (US & Canada)","geo_enabled":false,"verified":true,"statuses_count":13347,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"9AE4E8","profile_background_image_url":"http:\\/\\/a0.twimg.com\\/profile_background_images\\/55790865\\/Twitter_BG_Final_final.jpg","profile_background_image_url_https":"https:\\/\\/si0.twimg.com\\/profile_background_images\\/55790865\\/Twitter_BG_Final_final.jpg","profile_background_tile":false,"profile_image_url":"http:\\/\\/a0.twimg.com\\/profile_images\\/553327850\\/Eustacewbutterfly_normal.png","profile_image_url_https":"https:\\/\\/si0.twimg.com\\/profile_images\\/553327850\\/Eustacewbutterfly_normal.png","profile_banner_url":"https:\\/\\/pbs.twimg.com\\/profile_banners\\/14677919\\/1355412486","profile_link_color":"0084B4","profile_sidebar_border_color":"BDDCAD","profile_sidebar_fill_color":"DDFFCC","profile_text_color":"333333","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":false,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":10,"favorite_count":9,"entities":{"hashtags":[],"symbols":[],"urls":[{"url":"http:\\/\\/t.co\\/JMOeHGdglm","expanded_url":"http:\\/\\/nyr.kr\\/1eNhF2w","display_url":"nyr.kr\\/1eNhF2w","indices":[117,139]}],"user_mentions":[{"screen_name":"meghanor","name":"Meghan O'Rourke","id":35934858,"id_str":"35934858","indices":[62,71]}]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"lang":"en"}]" 

hmmm… so what else can you get with this method? just curious… there must be a lot of diff params you can add to that url…:wink:
(can you tell it to display less info/content??? :wink:

(it works just as well with absolute url, by the way…:wink:

hmm… so I have done up to step 3 here,

hopefully the rest will answer some of my other questions…:wink:

Yeah, if JSON decoding is on you get an array of objects, so you can just loop through the tweets:


foreach ($tweets as $tweet) {
    echo $tweet->text;
}

You’re pretty much there. I’d pass a search string to the PHP script, query the Twitter API, and then loop through the results and display them however you want. You could do it all with PHP if you wanted, or, like I mentioned before, you could use AJAX to call the PHP script and avoid the page refresh. If you returned the results as JSON you could update the page dynamically.

hmmm… “query the Twitter API…” ???

do I also use


$tweets = $twitter->get(.....)

also, I need to seach all tweets, universally, not just the ones associated with one account…

again, thank you for your help…

Yes, you just change the URL depending on which part of the API you’re talking to. From the page you linked to in your first post, the search url seems to be https://api.twitter.com/1.1/search/tweets.json?q=, so you would do something like:

$tweets = $connection->get('search/tweets.json?q='.$search_terms);

No worries, hope it gets you on the right track :slight_smile:

thank you fretburner…

so I was looking at how to display the results… and found this


https://api.twitter.com/1.1/search/tweets.json?q=@FreshPictures&result_type=recent&count=100&include_entities=true

here, https://dev.twitter.com/discussions/11407

is there documentation anywhere as to all the params you can pass to these urls?? (and how many urls are there? so far I have two:

  1. https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name
  2. https://api.twitter.com/1.1/search/tweets.json?q=

so are there others? again, would love to find some documentation…

I actually found this…

https://twitter.com/#!/search/%40twitterapi

here, https://dev.twitter.com/docs/using-search

so I wrote the simplest, most rudimentary search “app”… I just composed the url with what user enters in search text field, do window.location – and voila!! it works too… you land on a Twitter search results page…:slight_smile:
(and you don’t need an API for it…:wink:

thanks again…

I’m actually getting this error now

Notice: Undefined variable: connection in /Library/WebServer/Documents/_tw/search.php on line 42 Fatal error: Call to a member function get() on a non-object in /Library/WebServer/Documents/_tw/search.php on line 42

on this code…

$tweets = $connection->get('http://search.twitter.com/search.json?q=Apple');

and also on this one…

$tweets = $connection->get('http://search.twitter.com/search.json?q=the%rose&rpp=5&include_entities=true&result_type=recent');

The documentation for the API is at https://dev.twitter.com/docs/api/1.1 - you’ll see a list of all the resources (URLs) that you can access, and each one links to a page which gives examples and lists the parameters you can use.

Sorry, $connection is the variable I use in my script (I also use this library, to power the twitter widget on a website of mine), change it to $twitter and you should be good to go.