【问题标题】:I can't seem to be able to pull the tweets using the Twitter API method .. where did I go wrong?我似乎无法使用 Twitter API 方法提取推文。我哪里出错了?
【发布时间】:2012-12-10 14:06:35
【问题描述】:

所以我正在测试变量是否返回指定用户的屏幕名称 tweets 的 JSON 数据.. 但它没有返回任何内容,我使用了正确的 get 方法.. 我哪里出错了.. 下面是代码..

 <?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 返回这个

 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

【问题讨论】:

  • 您是否检查过您的互联网连接或代理设置(如果您正在使用它们)?您可以从浏览器访问 twitter.com 吗?
  • 在浏览器中粘贴url,看看是否在浏览器中得到输出
  • @praveen 是的,浏览器上的 URL 返回数据.. 但代码不会

标签: php twitter


【解决方案1】:

好吧,您从未将该请求发送到 twitter 服务器。您所做的只是创建了一个 url 字符串,然后使用 json_decode 相同的字符串。没有调用 API 函数,因此可以返回一些数据。

在您的浏览器中转到您创建的 url 字符串。您将获得 JSON 输出。

您要么必须使用 API 函数并将此 url 传递给该函数,以便它返回其他内容,请使用 curl 手动向该 url 发出请求并获取数据,然后 json_decode 它。

编辑:使用已编辑的问题,我无法重现该错误。它在这里工作正常。查看此输出。

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 

注意:检查链接 file_get_contents throws 400 Bad Request error PHP & https://dev.twitter.com/discussions/678

可能你已经达到了当天的 api 限制,这就是他们阻止你并向你发送 400 响应的原因。

【讨论】:

  • 那么你是如何让 var_dump 返回数组的?你能告诉我代码哪里出错了吗???
  • 您的代码似乎没有问题。我只是复制粘贴它,它返回的结果很好。
  • 对我来说它根本不起作用......编辑的方法..它给了我这个..警告:file_get_contents(api.twitter.com/1/statuses/…)[function.file-get-contents]:无法打开流:HTTP 请求失败! HTTP/1.0 400 Bad Request in /home/content/12/9187412/html/madscore/index3.php on line 129 NULL
  • twitter.com 在浏览器上可以正常打开 .. 它只工作了一次并给了我错误 .. 你认为它有什么问题?
猜你喜欢
  • 1970-01-01
  • 2013-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多