0

So I am testing if the variable returns the JSON data of the specified user's screen name tweets .. but it doesn't return anything and I am using the right get method .. Where did I go wrong .. Below is the code ..

 <?php
$url= "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=agasaro&count=5";
$results = json_decode(file_get_contents($url));
var_dump($results);

VAR_DUMP returns this

 Warning: file_get_contents(http://api.twitter.com/1/statuses/user_timeline.json?screen_name=agasaro&count=5) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in
Jimbo
  • 25,790
  • 15
  • 86
  • 131
Guy Rwayitare
  • 73
  • 2
  • 6

1 Answers1

0

Well you never sent that request to the twitter server.. What you did is just created a url string and then just used json_decode that same string. No API function was called so some data can be returned.

In your browser go to the url string you made. You will get the JSON output.

Either you must use an API function and pass this url to that funciton so that it returns something else use curl to do a request to that url manually and get the data and then json_decode it.

EDIT : With the edited question, I cant reproduce the error. Its working fine here. see this output.

array (size=2)
0 => 
object(stdClass)[2]
  public 'created_at' => string 'Thu Dec 20 11:11:29 +0000 2012' (length=30)
  public 'id' => float 2.8171845961476E+17
  public 'id_str' => string '281718459614760960' (length=18)
  public 'text' => string 'RPF’S 25-YEAR JOURNEY - The New Times Rwanda http://t.co/BvEuZz06 via @NewTimesRwanda' (length=87)
  public 'source' => string '<a href="http://twitter.com/tweetbutton" rel="nofollow">Tweet Button</a>' (length=72)
  public 'truncated' => boolean false
  public 'in_reply_to_status_id' => null
  public 'in_reply_to_status_id_str' => null
  public 'in_reply_to_user_id' => null
  public 'in_reply_to_user_id_str' => null
  public 'in_reply_to_screen_name' => null
  public 'user' => 
    object(stdClass)[3]
      public 'id' => int 17686705
      public 'id_str' => string '17686705' (length=8)
      public 'name' => string 'Angelica Gasaro' (length=15)
      public 'screen_name' => string 'agasaro' (length=7)
      public 'location' => string 'SW florida' (length=10)
      public 'url' => null
      public 'description' => string '' (length=0)
      public 'protected' => boolean false
      public 'followers_count' => int 63
      public 'friends_count' => int 43
      public 'listed_count' => int 0
      public 'created_at' => string 'Thu Nov 27 19:42:59 +0000 2008' (length=30)
      public 'favourites_count' => int 5
      public 'utc_offset' => int -18000
      public 'time_zone' => string 'Eastern Time (US & Canada)' (length=26)
      public 'geo_enabled' => boolean true
      public 'verified' => boolean false
      public 'statuses_count' => int 70
      public 'lang' => string 'en' (length=2)
      public 'contributors_enabled' => boolean false
      public 'is_translator' => boolean false
      public 'profile_background_color' => string 'B2DFDA' (length=6)
      public 'profile_background_image_url' => string 'http://a0.twimg.com/images/themes/theme13/bg.gif' (length=48)
      public 'profile_background_image_url_https' => string 'https://si0.twimg.com/images/themes/theme13/bg.gif' (length=50)
      public 'profile_background_tile' => boolean false
      public 'profile_image_url' => string 'http://a0.twimg.com/profile_images/2849801480/1d045f13f7b8d248516900a1960be54c_normal.jpeg' (length=90)
      public 'profile_image_url_https' => string 'https://si0.twimg.com/profile_images/2849801480/1d045f13f7b8d248516900a1960be54c_normal.jpeg' (length=92)
      public 'profile_banner_url' => string 'https://si0.twimg.com/profile_banners/17686705/1352931714' (length=57)
      public 'profile_link_color' => string '93A644' (length=6)
      public 'profile_sidebar_border_color' => string 'EEEEEE' (length=6)
      public 'profile_sidebar_fill_color' => string 'FFFFFF' (length=6)
      public 'profile_text_color' => string '333333' (length=6)
      public 'profile_use_background_image' => boolean true
      public 'default_profile' => boolean false
      public 'default_profile_image' => boolean false
      public 'following' => null
      public 'follow_request_sent' => null
      public 'notifications' => null
  public 'geo' => null
  public 'coordinates' => null
  public 'place' => null
  public 'contributors' => null
  public 'retweet_count' => int 0
  public 'favorited' => boolean false
  public 'retweeted' => boolean false
  public 'possibly_sensitive' => boolean false
1 => 
object(stdClass)[4]
  public 'created_at' => string 'Sun Dec 09 15:19:29 +0000 2012' (length=30)
  public 'id' => float 2.777946036069E+17
  public 'id_str' => string '277794603606896640' (length=18)
  public 'text' => string '“@RichardDreyfuss: The Island Where People Forget to Die http://t.co/rhN3s5E9â€' (length=82)
  public 'source' => string '<a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>' (length=82)
  public 'truncated' => boolean false
  public 'in_reply_to_status_id' => null
  public 'in_reply_to_status_id_str' => null
  public 'in_reply_to_user_id' => null
  public 'in_reply_to_user_id_str' => null
  public 'in_reply_to_screen_name' => null
  public 'user' => 
    object(stdClass)[5]
      public 'id' => int 17686705
      public 'id_str' => string '17686705' (length=8)
      public 'name' => string 'Angelica Gasaro' (length=15)
      public 'screen_name' => string 'agasaro' (length=7)
      public 'location' => string 'SW florida' (length=10)
      public 'url' => null
      public 'description' => string '' (length=0)
      public 'protected' => boolean false
      public 'followers_count' => int 63
      public 'friends_count' => int 43
      public 'listed_count' => int 0
      public 'created_at' => string 'Thu Nov 27 19:42:59 +0000 2008' (length=30)
      public 'favourites_count' => int 5
      public 'utc_offset' => int -18000
      public 'time_zone' => string 'Eastern Time (US & Canada)' (length=26)
      public 'geo_enabled' => boolean true
      public 'verified' => boolean false
      public 'statuses_count' => int 70
      public 'lang' => string 'en' (length=2)
      public 'contributors_enabled' => boolean false
      public 'is_translator' => boolean false
      public 'profile_background_color' => string 'B2DFDA' (length=6)
      public 'profile_background_image_url' => string 'http://a0.twimg.com/images/themes/theme13/bg.gif' (length=48)
      public 'profile_background_image_url_https' => string 'https://si0.twimg.com/images/themes/theme13/bg.gif' (length=50)
      public 'profile_background_tile' => boolean false
      public 'profile_image_url' => string 'http://a0.twimg.com/profile_images/2849801480/1d045f13f7b8d248516900a1960be54c_normal.jpeg' (length=90)
      public 'profile_image_url_https' => string 'https://si0.twimg.com/profile_images/2849801480/1d045f13f7b8d248516900a1960be54c_normal.jpeg' (length=92)
      public 'profile_banner_url' => string 'https://si0.twimg.com/profile_banners/17686705/1352931714' (length=57)
      public 'profile_link_color' => string '93A644' (length=6)
      public 'profile_sidebar_border_color' => string 'EEEEEE' (length=6)
      public 'profile_sidebar_fill_color' => string 'FFFFFF' (length=6)
      public 'profile_text_color' => string '333333' (length=6)
      public 'profile_use_background_image' => boolean true
      public 'default_profile' => boolean false
      public 'default_profile_image' => boolean false
      public 'following' => null
      public 'follow_request_sent' => null
      public 'notifications' => null
  public 'geo' => null
  public 'coordinates' => null
  public 'place' => null
  public 'contributors' => null
  public 'retweet_count' => int 0
  public 'favorited' => boolean false
  public 'retweeted' => boolean false
  public 'possibly_sensitive' => boolean false 

NOTE : check the links file_get_contents throws 400 Bad Request error PHP & https://dev.twitter.com/discussions/678

May be you have reached the api limit for the day thats why they are blocking you and sending you a 400 response.

Community
  • 1
  • 1
Praveen Puglia
  • 5,577
  • 5
  • 34
  • 68
  • so how did you get the var_dump to return the array ?? can you tell me exactly where I went wrong in the code ??? – Guy Rwayitare Dec 24 '12 at 02:52
  • Nothing seems to be wrong with your code. i just copy pasted it and it returned the results fine. – Praveen Puglia Dec 24 '12 at 02:55
  • for me it's not working at all ... the edited method .. it gives me this .. Warning: file_get_contents(http://api.twitter.com/1/statuses/user_timeline.json?screen_name=MASE_85&count=5) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /home/content/12/9187412/html/madscore/index3.php on line 129 NULL – Guy Rwayitare Dec 24 '12 at 03:02
  • twitter.com opens fine on the browser .. It worked only one time and gave me back the error .. what do u think could be wrong with it ? – Guy Rwayitare Dec 24 '12 at 03:03