doudun2212 2018-11-21 15:13
浏览 56

PHP - 如何使用Twitter API获取推文ID

I am trying to work with the twitter API using Abraham's twitteroauth, I posted a tweet but now I need to get the ID of said tweet, I have been trying to look for a way to get the ID of a tweet as soon as I post it, but can't seem to find a way to do it that works for me.

The following code is what I am using to get the ID, but I get nothing from that that echo, just a blank "tweet ID: "

$postResult= $twitter->post('statuses/update', $parameters);
$test = json_decode($postResult->response);
$tweetID = $test->id_str;
echo "Tweet ID: " . $tweetID;

Printing the $postResult gives me the whole object (a huge mess to look at for someone with little experience like me) but I am not sure if I am doing something wrong (probably am) when trying to get the ID out of it, if you guys happen to know a simple way to do it I would love to know about it.

Also if you happen to know how I could retrieve the account's @ScreenName that would be nice too...

Thanks!

EDIT: Here's what I get from $postResult:

object(stdClass)#22 (25) { ["created_at"]=> string(30) "Wed Nov 21 17:03:46 +0000 2018" ["id"]=> string(19) "1065289678629322752" ["id_str"]=> string(19) "1065289678629322752" ["text"]=> string(29) "asdfk" ["truncated"]=> bool(false) ["entities"]=> object(stdClass)#23 (5) { ["hashtags"]=> array(0) { } ["symbols"]=> array(0) { } ["user_mentions"]=> array(0) { } ["urls"]=> array(0) { } ["media"]=> array(1) { [0]=> object(stdClass)#24 (10) { ["id"]=> string(19) "1065289676171427841" ["id_str"]=> string(19) "1065289676171427841" ["indices"]=> array(2) { [0]=> int(6) [1]=> int(29) } ["media_url"]=> string(46) "" ["media_url_https"]=> string(47) "" ["url"]=> string(23) "" ["display_url"]=> string(26) "" ["expanded_url"]=> string(62) "" ["type"]=> string(5) "photo" ["sizes"]=> object(stdClass)#26 (4) { ["thumb"]=> object(stdClass)#25 (3) { ["w"]=> int(150) ["h"]=> int(150) ["resize"]=> string(4) "crop" } ["small"]=> object(stdClass)#27 (3) { ["w"]=> int(453) ["h"]=> int(680) ["resize"]=> string(3) "fit" } ["medium"]=> object(stdClass)#28 (3) { ["w"]=> int(500) ["h"]=> int(750) ["resize"]=> string(3) "fit" } ["large"]=> object(stdClass)#29 (3) { ["w"]=> int(500) ["h"]=> int(750) ["resize"]=> string(3) "fit" } } } } } ["extended_entities"]=> object(stdClass)#36 (1) { ["media"]=> array(1) { [0]=> object(stdClass)#30 (10) { ["id"]=> string(19) "1065289676171427841" ["id_str"]=> string(19) "1065289676171427841" ["indices"]=> array(2) { [0]=> int(6) [1]=> int(29) } ["media_url"]=> string(46) "" ["media_url_https"]=> string(47) "" ["url"]=> string(23) "" ["display_url"]=> string(26) "" ["expanded_url"]=> string(62) "" ["type"]=> string(5) "photo" ["sizes"]=> object(stdClass)#32 (4) { ["thumb"]=> object(stdClass)#31 (3) { ["w"]=> int(150) ["h"]=> int(150) ["resize"]=> string(4) "crop" } ["small"]=> object(stdClass)#33 (3) { ["w"]=> int(453) ["h"]=> int(680) ["resize"]=> string(3) "fit" } ["medium"]=> object(stdClass)#34 (3) { ["w"]=> int(500) ["h"]=> int(750) ["resize"]=> string(3) "fit" } ["large"]=> object(stdClass)#35 (3) { ["w"]=> int(500) ["h"]=> int(750) ["resize"]=> string(3) "fit" } } } } } ["source"]=> string(67) "ITSales S.A." ["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)#37 (42) { ["id"]=> string(10) "4377059237" ["id_str"]=> string(10) "4377059237" ["name"]=> string(15) "random follower" ["screen_name"]=> string(7) "itsmil0" ["location"]=> string(0) "" ["description"]=> string(3) ":^)" ["url"]=> NULL ["entities"]=> object(stdClass)#39 (1) { ["description"]=> object(stdClass)#38 (1) { ["urls"]=> array(0) { } } } ["protected"]=> bool(false) ["followers_count"]=> int(20) ["friends_count"]=> int(538) ["listed_count"]=> int(0) ["created_at"]=> string(30) "Fri Dec 04 23:31:36 +0000 2015" ["favourites_count"]=> int(8744) ["utc_offset"]=> NULL ["time_zone"]=> NULL ["geo_enabled"]=> bool(false) ["verified"]=> bool(false) ["statuses_count"]=> int(295) ["lang"]=> string(2) "es" ["contributors_enabled"]=> bool(false) ["is_translator"]=> bool(false) ["is_translation_enabled"]=> bool(false) ["profile_background_color"]=> string(6) "000000" ["profile_background_image_url"]=> string(48) "" ["profile_background_image_url_https"]=> string(49) "" ["profile_background_tile"]=> bool(false) ["profile_image_url"]=> string(75) "" ["profile_image_url_https"]=> string(76) "" ["profile_banner_url"]=> string(59) "" ["profile_link_color"]=> string(6) "ABB8C2" ["profile_sidebar_border_color"]=> string(6) "000000" ["profile_sidebar_fill_color"]=> string(6) "000000" ["profile_text_color"]=> string(6) "000000" ["profile_use_background_image"]=> bool(false) ["has_extended_profile"]=> bool(true) ["default_profile"]=> bool(false) ["default_profile_image"]=> bool(false) ["following"]=> bool(false) ["follow_request_sent"]=> bool(false) ["notifications"]=> bool(false) ["translator_type"]=> string(4) "none" } ["geo"]=> NULL ["coordinates"]=> NULL ["place"]=> NULL ["contributors"]=> NULL ["is_quote_status"]=> bool(false) ["retweet_count"]=> int(0) ["favorite_count"]=> int(0) ["favorited"]=> bool(false) ["retweeted"]=> bool(false) ["possibly_sensitive"]=> bool(false) ["lang"]=> string(2) "tr" }

EDIT 2: I tried print_r() and got the following:

stdClass Object ( [created_at] => Wed Nov 21 19:34:37 +0000 2018 [id] => 1065327644034719746 [id_str] => 1065327644034719746 [text] => asdfk [truncated] => [entities] => stdClass Object ( [hashtags] => Array ( ) [symbols] => Array ( ) [user_mentions] => Array ( ) [urls] => Array ( ) [media] => Array ( [0] => stdClass Object ( [id] => 1065327641627160586 [id_str] => 1065327641627160586 [indices] => Array ( [0] => 6 [1] => 29 ) [media_url] => [media_url_https] => [url] => [display_url] => pic.twitter.com/L1iWuJlvWD [expanded_url] => [type] => photo [sizes] => stdClass Object ( [thumb] => stdClass Object ( [w] => 150 [h] => 150 [resize] => crop ) [small] => stdClass Object ( [w] => 453 [h] => 680 [resize] => fit ) [medium] => stdClass Object ( [w] => 500 [h] => 750 [resize] => fit ) [large] => stdClass Object ( [w] => 500 [h] => 750 [resize] => fit ) ) ) ) ) [extended_entities] => stdClass Object ( [media] => Array ( [0] => stdClass Object ( [id] => 1065327641627160586 [id_str] => 1065327641627160586 [indices] => Array ( [0] => 6 [1] => 29 ) [media_url] => [media_url_https] => [url] => [display_url] => [expanded_url] => [type] => photo [sizes] => stdClass Object ( [thumb] => stdClass Object ( [w] => 150 [h] => 150 [resize] => crop ) [small] => stdClass Object ( [w] => 453 [h] => 680 [resize] => fit ) [medium] => stdClass Object ( [w] => 500 [h] => 750 [resize] => fit ) [large] => stdClass Object ( [w] => 500 [h] => 750 [resize] => fit ) ) ) ) ) [source] => ITSales S.A. [in_reply_to_status_id] => [in_reply_to_status_id_str] => [in_reply_to_user_id] => [in_reply_to_user_id_str] => [in_reply_to_screen_name] => [user] => stdClass Object ( [id] => 4377059237 [id_str] => 4377059237 [name] => random follower [screen_name] => itsmil0 [location] => [description] => :^) [url] => [entities] => stdClass Object ( [description] => stdClass Object ( [urls] => Array ( ) ) ) [protected] => [followers_count] => 20 [friends_count] => 538 [listed_count] => 0 [created_at] => Fri Dec 04 23:31:36 +0000 2015 [favourites_count] => 8744 [utc_offset] => [time_zone] => [geo_enabled] => [verified] => [statuses_count] => 301 [lang] => es [contributors_enabled] => [is_translator] => [is_translation_enabled] => [profile_background_color] => 000000 [profile_background_image_url] => [profile_background_image_url_https] => [profile_background_tile] => [profile_image_url] => [profile_image_url_https] => https://pbs.twimg.com/profile_images/1061085195078696960/ME31cbVc_normal.jpg [profile_banner_url] => https://pbs.twimg.com/profile_banners/4377059237/1541088916 [profile_link_color] => ABB8C2 [profile_sidebar_border_color] => 000000 [profile_sidebar_fill_color] => 000000 [profile_text_color] => 000000 [profile_use_background_image] => [has_extended_profile] => 1 [default_profile] => [default_profile_image] => [following] => [follow_request_sent] => [notifications] => [translator_type] => none ) [geo] => [coordinates] => [place] => [contributors] => [is_quote_status] => [retweet_count] => 0 [favorite_count] => 0 [favorited] => [retweeted] => [possibly_sensitive] => [lang] => tr )

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 安装svn网络有问题怎么办
    • ¥15 Python爬取指定微博话题下的内容,保存为txt
    • ¥15 vue2登录调用后端接口如何实现
    • ¥65 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥15 latex怎么处理论文引理引用参考文献