SitePoint Sponsor

User Tag List

Results 1 to 21 of 21

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    823
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

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

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,414
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Put it after the call to the API:
    PHP Code:
    $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.

  3. #3
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    823
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    ok... am getting all this stuff now....

    Code:
    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? ;-) 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.....:-)

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,414
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    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:
    PHP Code:
    $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

  5. #5
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    823
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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....;-)
    (I love how you can get it returned as JSON.....;-)

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

  6. #6
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    823
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    so this what I get for just one tweet.... (if do count "1" on this line,

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

    Code:
    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...;-)
    (can you tell it to display less info/content??? ;-)

    (it works just as well with absolute url, by the way....;-)

  7. #7
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,414
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by maya90 View Post
    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....))
    Yeah, if JSON decoding is on you get an array of objects, so you can just loop through the tweets:
    PHP Code:
    foreach ($tweets as $tweet) {
        echo 
    $tweet->text;

    Quote Originally Posted by maya90 View Post
    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.....
    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.

  8. #8
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    However, should users also be signing into my app to make use of this functionality? In other words, should this feature only be offered to those with a Twitter account, who would need to sign in to my app in order to use the search functionality? Or is this extra layer of interactivity covered by the use of Web Intents?

    Any clarification would be most helpful.

  9. #9
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,414
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    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.

  10. #10
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    823
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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...;-)

    thank you......

  11. #11
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    823
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    fretburner,

    thank you for posting this.......
    http://www.sitepoint.com/twitter-1-1-oauth-php/

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

    but this code is not printing anything.....

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

  12. #12
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,414
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    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:
    PHP Code:
    $tweets $twitter->get('statuses/user_timeline.json?screen_name=NewYorker&count=10'); 

  13. #13
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    823
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code:
    $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....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •