Format Tweets in twitter api 1.1

2019-09-17 00:02发布

I am trying to upgrade my client's account to use the new Twitter API 1.1. I've done a lot of research, and found some code here on Stack Overflow that is listed here: https://github.com/J7mbo/twitter-api-php.

So, I've got the code working (for the most part) but instead of listing tweets, I get a list of code:

[{"created_at":"Wed Jun 12 13:06:28 +0000 2013","id":344802886762512386,"id_str":"344802886762512386","text":"Loading a #prefabarch for delivery the fun way #archkit #framing #razor https:\/\/t.co\/oh2WfbCPNi","source":"\u003ca href=\"http:\/\/vine.co\" rel=\"nofollow\"\u003eVine - Make a Scene\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":47689193,"id_str":"47689193","name":"Archways & Ceilings","screen_name":"Archking57","location":"Arlington, TX","description":"We supply framing kits for Dome Ceilings, Groin Vaults, Barrel Vaults, Coves, and Archways","url":"http:\/\/t.co\/FssCqf6lGz","entities":{"url":{"urls":[{"url":"http:\/\/t.co\/FssCqf6lGz","expanded_url":"http:\/\/www.archwaysandceilings.com","display_url":"archwaysandceilings.com","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":351,"friends_count":88,"listed_count":10,"created_at":"Tue Jun 16 17:48:23 +0000 2009","favourites_count":0,"utc_offset":-21600,"time_zone":"Central Time (US & Canada)","geo_enabled":true,"verified":false,"statuses_count":331,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"000505","profile_background_image_url":"http:\/\/a0.twimg.com\/profile_background_images\/223605213\/ACME-YOUTUBE-LOW-QUALITY.jpg","profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/223605213\/ACME-YOUTUBE-LOW-QUALITY.jpg","profile_background_tile":true,"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/1927262664\/ACME_Logo_normal.jpg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1927262664\/ACME_Logo_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/47689193\/1362599335","profile_link_color":"560AFA","profile_sidebar_border_color":"850D2B","profile_sidebar_fill_color":"020501","profile_text_color":"CF0830","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":0,"favorite_count":0,"entities":{"hashtags":[{"text":"prefabarch","indices":[10,21]},{"text":"archkit","indices":[47,55]},{"text":"framing","indices":[56,64]},{"text":"razor","indices":[65,71]}],"symbols":[],"urls":[{"url":"https:\/\/t.co\/oh2WfbCPNi","expanded_url":"https:\/\/vine.co\/v\/blvep6iJVzp","display_url":"vine.co\/v\/blvep6iJVzp","indices":[72,95]}],"user_mentions":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"lang":"en"},{"created_at":"Wed Jun 12 12:15:41 +0000 2013","id":344790103782207489,"id_str":"344790103782207489","text":"Learn how to design, plan and build arched ceilings http:\/\/t.co\/YXr6rVNnjL #vr4smallbiz","source":"\u003ca href=\"http:\/\/www.verticalresponse.com\" rel=\"nofollow\"\u003eVerticalResponse\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":47689193,"id_str":"47689193","name":"Archways & Ceilings","screen_name":"Archking57","location":"Arlington, TX","description":"We supply framing kits for Dome Ceilings, Groin Vaults, Barrel Vaults, Coves, and Archways","url":"http:\/\/t.co\/FssCqf6lGz","entities":{"url":{"urls":[{"url":"http:\/\/t.co\/FssCqf6lGz","expanded_url":"http:\/\/www.archwaysandceilings.com","display_url":"archwaysandceilings.com","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":351,"friends_count":88,"listed_count":10,"created_at":"Tue Jun 16 17:48:23 +0000 2009","favourites_count":0,"utc_offset":-21600,"time_zone":"Central Time (US & Canada)","geo_enabled":true,"verified":false,"statuses_count":331,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"000505","profile_background_image_url":"http:\/\/a0.twimg.com\/profile_background_images\/223605213\/ACME-YOUTUBE-LOW-QUALITY.jpg","profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/223605213\/ACME-YOUTUBE-LOW-QUALITY.jpg","profile_background_tile":true,"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/1927262664\/ACME_Logo_normal.jpg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/1927262664\/ACME_Logo_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/47689193\/1362599335","profile_link_color":"560AFA","profile_sidebar_border_color":"850D2B","profile_sidebar_fill_color":"020501","profile_text_color":"CF0830","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":0,"favorite_count":0,"entities":{"hashtags":[{"text":"vr4smallbiz","indices":[75,87]}],"symbols":[],"urls":[{"url":"http:\/\/t.co\/YXr6rVNnjL","expanded_url":"http:\/\/p0.vresp.com\/l2fZaa","display_url":"p0.vresp.com\/l2fZaa","indices":[52,74]}],"user_mentions":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"lang":"en"}]

I can't figure out where I went wrong! Below is my PHP code:

require_once('include/twitter-api-php-master/TwitterAPIExchange.php');
$settings = array(
    'oauth_access_token' => "MYKEY",
    'oauth_access_token_secret' => "MYKEY",
    'consumer_key' => "MYKEY",
    'consumer_secret' => "MYKEY"
);
 $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
 $getfield = '?screen_name=archking57&count=2&exclude_replies=true';
 $requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
             ->buildOauth($url, $requestMethod)
             ->performRequest();

So my question is why isn't it showing just the tweets? How can I format it into tweets?

标签: php twitter
1条回答
我只想做你的唯一
2楼-- · 2019-09-17 00:37

So what you're getting back is json, which is the standard twitter API response format.

Using PHP's json_decode():

$result = $twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest();
var_dump(json_decode($result));`

You'll get something that looks like this:

enter image description here

Does that look more like the data you're after?

查看更多
登录 后发表回答