doupin8555 2015-08-08 11:59
浏览 121
已采纳

PHP:Twitter搜索API - 如何从JSON数据获取推文

I have a twitter php search script with twitter search api.

My code is here:

<?php
require_once('TwitterAPIExchange.php');

$settings = array(
    'oauth_access_token' => "XXX",
    'oauth_access_token_secret' => "XXX",
    'consumer_key' => "XXX",
    'consumer_secret' => "XXX"
);

$url = "https://api.twitter.com/1.1/search/tweets.json";
$requestMethod = "GET";
$getfield = '?f=tweets&vertical=default&q=wish&src=typd';
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
    ->buildOauth($url, $requestMethod)
    ->performRequest(),$assoc = TRUE);

echo "<pre>";
print_r($string);
echo "</pre>";
?>

And it return to me a sample search result with json. Let me give an example result:

Array
(
    [statuses] => Array
        (
            [0] => Array
                (
                    [metadata] => Array
                        (
                            [iso_language_code] => en
                            [result_type] => recent
                        )

                    [created_at] => Sat Aug 08 11:54:02 +0000 2015
                    [id] => 629983908336005122
                    [id_str] => 629983908336005122
                    [text] => RT @pilosopo_tasho: "Sometimes I wish I could unmeet, delete, forget people."
                    [source] => Twitter for Android
                    [truncated] => 
                    [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] => Array
                        (
                            [id] => 1734364392
                            [id_str] => 1734364392
                            [name] => EL EDWARD
                            [screen_name] => qwerty12307
                            [location] => Tanauan City,Batangas
                            [description] => | BornAgain | Drummer | Rakker | 7 is ♥ | PogiSInceBirthxD | Artist | God's Servant & Believer | PARAMORE LOVER | https://t.co/CD0EQkiOUM
                            [url] => 
                            [entities] => Array
                                (
                                    [description] => Array
                                        (
                                            [urls] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [url] => https://t.co/CD0EQkiOUM
                                                            [expanded_url] => https://www.facebook.com/WkM.Bobi
                                                            [display_url] => facebook.com/WkM.Bobi
                                                            [indices] => Array
                                                                (
                                                                    [0] => 114
                                                                    [1] => 137
                                                                )

                                                        )

                                                )

                                        )

                                )

                            [protected] => 
                            [followers_count] => 671
                            [friends_count] => 274
                            [listed_count] => 2
                            [created_at] => Fri Sep 06 07:56:03 +0000 2013
                            [favourites_count] => 1050
                            [utc_offset] => 28800
                            [time_zone] => Beijing
                            [geo_enabled] => 
                            [verified] => 
                            [statuses_count] => 6590
                            [lang] => fil
                            [contributors_enabled] => 
                            [is_translator] => 
                            [is_translation_enabled] => 
                            [profile_background_color] => 89C9FA
                            [profile_background_image_url] => http://pbs.twimg.com/profile_background_images/518335320815714304/IP4m7tYf.jpeg
                            [profile_background_image_url_https] => https://pbs.twimg.com/profile_background_images/518335320815714304/IP4m7tYf.jpeg
                            [profile_background_tile] => 1
                            [profile_image_url] => http://pbs.twimg.com/profile_images/629497679664381952/rcARf_sr_normal.jpg
                            [profile_image_url_https] => https://pbs.twimg.com/profile_images/629497679664381952/rcARf_sr_normal.jpg
                            [profile_banner_url] => https://pbs.twimg.com/profile_banners/1734364392/1438919080
                            [profile_link_color] => 3B94D9
                            [profile_sidebar_border_color] => 000000
                            [profile_sidebar_fill_color] => DDEEF6
                            [profile_text_color] => 333333
                            [profile_use_background_image] => 1
                            [has_extended_profile] => 
                            [default_profile] => 
                            [default_profile_image] => 
                            [following] => 
                            [follow_request_sent] => 
                            [notifications] => 
                        )

                    [geo] => 
                    [coordinates] => 
                    [place] => 
                    [contributors] => 
                    [retweeted_status] => Array
                        (
                            [metadata] => Array
                                (
                                    [iso_language_code] => en
                                    [result_type] => recent
                                )

                            [created_at] => Sat Aug 08 11:45:24 +0000 2015
                            [id] => 629981732717113344
                            [id_str] => 629981732717113344
                            [text] => "Sometimes I wish I could unmeet, delete, forget people."
                            [source] => Hootsuite
                            [truncated] => 
                            [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] => Array
                                (
                                    [id] => 938887459
                                    [id_str] => 938887459
                                    [name] => Pilosopo Tasyo
                                    [screen_name] => pilosopo_tasho
                                    [location] => Pilipinas
                                    [description] => Matandang Pilosopo | Member: United Influencers | Email: contact@bobongquote.com
                                    [url] => http://t.co/l52iYnJ89G
                                    [entities] => Array
                                        (
                                            [url] => Array
                                                (
                                                    [urls] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [url] => http://t.co/l52iYnJ89G
                                                                    [expanded_url] => http://www.bobongquote.com
                                                                    [display_url] => bobongquote.com
                                                                    [indices] => Array
                                                                        (
                                                                            [0] => 0
                                                                            [1] => 22
                                                                        )

                                                                )

                                                        )

                                                )

                                            [description] => Array
                                                (
                                                    [urls] => Array
                                                        (
                                                        )

                                                )

                                        )

                                    [protected] => 
                                    [followers_count] => 370638
                                    [friends_count] => 101
                                    [listed_count] => 186
                                    [created_at] => Sat Nov 10 11:43:47 +0000 2012
                                    [favourites_count] => 3
                                    [utc_offset] => 28800
                                    [time_zone] => Beijing
                                    [geo_enabled] => 1
                                    [verified] => 
                                    [statuses_count] => 52812
                                    [lang] => en
                                    [contributors_enabled] => 
                                    [is_translator] => 
                                    [is_translation_enabled] => 
                                    [profile_background_color] => 022330
                                    [profile_background_image_url] => http://abs.twimg.com/images/themes/theme15/bg.png
                                    [profile_background_image_url_https] => https://abs.twimg.com/images/themes/theme15/bg.png
                                    [profile_background_tile] => 
                                    [profile_image_url] => http://pbs.twimg.com/profile_images/577984458206281728/eOxv_H1X_normal.jpeg
                                    [profile_image_url_https] => https://pbs.twimg.com/profile_images/577984458206281728/eOxv_H1X_normal.jpeg
                                    [profile_banner_url] => https://pbs.twimg.com/profile_banners/938887459/1426637219
                                    [profile_link_color] => 0084B4
                                    [profile_sidebar_border_color] => A8C7F7
                                    [profile_sidebar_fill_color] => C0DFEC
                                    [profile_text_color] => 333333
                                    [profile_use_background_image] => 1
                                    [has_extended_profile] => 
                                    [default_profile] => 
                                    [default_profile_image] => 
                                    [following] => 
                                    [follow_request_sent] => 
                                    [notifications] => 
                                )

                            [geo] => 
                            [coordinates] => 
                            [place] => 
                            [contributors] => 
                            [is_quote_status] => 
                            [retweet_count] => 66
                            [favorite_count] => 59
                            [entities] => Array
                                (
                                    [hashtags] => Array
                                        (
                                        )

                                    [symbols] => Array
                                        (
                                        )

                                    [user_mentions] => Array
                                        (
                                        )

                                    [urls] => Array
                                        (
                                        )

                                )

                            [favorited] => 
                            [retweeted] => 
                            [lang] => en
                        )

                    [is_quote_status] => 
                    [retweet_count] => 66
                    [favorite_count] => 0
                    [entities] => Array
                        (
                            [hashtags] => Array
                                (
                                )

                            [symbols] => Array
                                (
                                )

                            [user_mentions] => Array
                                (
                                    [0] => Array
                                        (
                                            [screen_name] => pilosopo_tasho
                                            [name] => Pilosopo Tasyo
                                            [id] => 938887459
                                            [id_str] => 938887459
                                            [indices] => Array
                                                (
                                                    [0] => 3
                                                    [1] => 18
                                                )

                                        )

                                )

                            [urls] => Array
                                (
                                )

                        )

                    [favorited] => 
                    [retweeted] => 
                    [lang] => en
                )

            [1] => Array
                (
                    [metadata] => Array
                        (
                            [iso_language_code] => en
                            [result_type] => recent
                        )

                    [created_at] => Sat Aug 08 11:54:02 +0000 2015
                    [id] => 629983907157446656
                    [id_str] => 629983907157446656
                    [text] => RT @disneywords: The most fantastic, magical things can happen, and it all starts with a wish. –Jiminy Cricket (Pinocchio)
                    [source] => Twitter for Android
                    [truncated] => 
                    [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] => Array
                        (
                            [id] => 960524995
                            [id_str] => 960524995
                            [name] => Sofi
                            [screen_name] => sofinur_
                            [location] => 
                            [description] => dreamer
                            [url] => 
                            [entities] => Array
                                (
                                    [description] => Array
                                        (
                                            [urls] => Array
                                                (
                                                )

                                        )

                                )

                            [protected] => 
                            [followers_count] => 205
                            [friends_count] => 256
                            [listed_count] => 1
                            [created_at] => Tue Nov 20 13:52:03 +0000 2012
                            [favourites_count] => 20
                            [utc_offset] => 25200
                            [time_zone] => Bangkok
                            [geo_enabled] => 1
                            [verified] => 
                            [statuses_count] => 3096
                            [lang] => id
                            [contributors_enabled] => 
                            [is_translator] => 
                            [is_translation_enabled] => 
                            [profile_background_color] => 352726
                            [profile_background_image_url] => http://abs.twimg.com/images/themes/theme5/bg.gif
                            [profile_background_image_url_https] => https://abs.twimg.com/images/themes/theme5/bg.gif
                            [profile_background_tile] => 
                            [profile_image_url] => http://pbs.twimg.com/profile_images/629941184132087809/0NtWuWk8_normal.jpg
                            [profile_image_url_https] => https://pbs.twimg.com/profile_images/629941184132087809/0NtWuWk8_normal.jpg
                            [profile_banner_url] => https://pbs.twimg.com/profile_banners/960524995/1434944169
                            [profile_link_color] => 6B340A
                            [profile_sidebar_border_color] => FFFFFF
                            [profile_sidebar_fill_color] => EADEAA
                            [profile_text_color] => 333333
                            [profile_use_background_image] => 1
                            [has_extended_profile] => 
                            [default_profile] => 
                            [default_profile_image] => 
                            [following] => 
                            [follow_request_sent] => 
                            [notifications] => 
                        )

                    [geo] => 
                    [coordinates] => 
                    [place] => 
                    [contributors] => 
                    [retweeted_status] => Array
                        (
                            [metadata] => Array
                                (
                                    [iso_language_code] => en
                                    [result_type] => recent
                                )

                            [created_at] => Sat Aug 08 11:45:39 +0000 2015
                            [id] => 629981798701920256
                            [id_str] => 629981798701920256
                            [text] => The most fantastic, magical things can happen, and it all starts with a wish. –Jiminy Cricket (Pinocchio)
                            [source] => Hootsuite
                            [truncated] => 
                            [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] => Array
                                (
                                    [id] => 126400767
                                    [id_str] => 126400767
                                    [name] => Disney Words
                                    [screen_name] => disneywords
                                    [location] => disneywordstwitter@yahoo.com
                                    [description] => We can learn a lot of things from Disney characters, and sometimes they just help us express what we feel.
                                    [url] => http://t.co/d5O0KztA4I
                                    [entities] => Array
                                        (
                                            [url] => Array
                                                (
                                                    [urls] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [url] => http://t.co/d5O0KztA4I
                                                                    [expanded_url] => http://disneywords.me
                                                                    [display_url] => disneywords.me
                                                                    [indices] => Array
                                                                        (
                                                                            [0] => 0
                                                                            [1] => 22
                                                                        )

                                                                )

                                                        )

                                                )

                                            [description] => Array
                                                (
                                                    [urls] => Array
                                                        (
                                                        )

                                                )

                                        )

                                    [protected] => 
                                    [followers_count] => 3525208
                                    [friends_count] => 0
                                    [listed_count] => 7930
                                    [created_at] => Thu Mar 25 19:13:32 +0000 2010
                                    [favourites_count] => 0
                                    [utc_offset] => -25200
                                    [time_zone] => Pacific Time (US & Canada)
                                    [geo_enabled] => 1
                                    [verified] => 
                                    [statuses_count] => 82010
                                    [lang] => en
                                    [contributors_enabled] => 
                                    [is_translator] => 
                                    [is_translation_enabled] => 
                                    [profile_background_color] => 030824
                                    [profile_background_image_url] => http://pbs.twimg.com/profile_background_images/676942340/92934d4a0422a2e865f16a51bfc1fc65.jpeg
                                    [profile_background_image_url_https] => https://pbs.twimg.com/profile_background_images/676942340/92934d4a0422a2e865f16a51bfc1fc65.jpeg
                                    [profile_background_tile] => 
                                    [profile_image_url] => http://pbs.twimg.com/profile_images/1175908443/disneywordspp_normal.jpg
                                    [profile_image_url_https] => https://pbs.twimg.com/profile_images/1175908443/disneywordspp_normal.jpg
                                    [profile_banner_url] => https://pbs.twimg.com/profile_banners/126400767/1401374801
                                    [profile_link_color] => 0C2352
                                    [profile_sidebar_border_color] => FFFFFF
                                    [profile_sidebar_fill_color] => 64738F
                                    [profile_text_color] => 020A1F
                                    [profile_use_background_image] => 1
                                    [has_extended_profile] => 
                                    [default_profile] => 
                                    [default_profile_image] => 
                                    [following] => 
                                    [follow_request_sent] => 
                                    [notifications] => 
                                )

                            [geo] => 
                            [coordinates] => 
                            [place] => 
                            [contributors] => 
                            [is_quote_status] => 
                            [retweet_count] => 85
                            [favorite_count] => 62
                            [entities] => Array
                                (
                                    [hashtags] => Array
                                        (
                                        )

                                    [symbols] => Array
                                        (
                                        )

                                    [user_mentions] => Array
                                        (
                                        )

                                    [urls] => Array
                                        (
                                        )

                                )

                            [favorited] => 
                            [retweeted] => 
                            [lang] => en
                        )

                    [is_quote_status] => 
                    [retweet_count] => 85
                    [favorite_count] => 0
                    [entities] => Array
                        (
                            [hashtags] => Array
                                (
                                )

                            [symbols] => Array
                                (
                                )

                            [user_mentions] => Array
                                (
                                    [0] => Array
                                        (
                                            [screen_name] => disneywords
                                            [name] => Disney Words
                                            [id] => 126400767
                                            [id_str] => 126400767
                                            [indices] => Array
                                                (
                                                    [0] => 3
                                                    [1] => 15
                                                )

                                        )

                                )

                            [urls] => Array
                                (
                                )

                        )

                    [favorited] => 
                    [retweeted] => 
                    [lang] => en
                )..........

A part of my search JSON result is above.

Now I want to get just tweets ([text]) from this result.

How to get all [text] values with php from this JSON?

  • 写回答

1条回答 默认 最新

  • dony39517 2015-08-08 14:56
    关注

    First of all, I've changed some part of code in the question. Because it was wrong when refer the json api.

    $string = json_decode($twitter->setGetfield($getfield)
    ->buildOauth($url, $requestMethod)
    ->performRequest(),$assoc = TRUE);
    

    this part changed by this

    $string = json_decode($twitter->setGetfield($getfield)
        ->buildOauth($url, $requestMethod)
        ->performRequest(), true);
    

    Then, need a loop for get all tweets from the json array. For solution:

    foreach($string['statuses'] as $tweets) {
        echo $tweets['text'] . '<br>';
    
    }
    

    It works on me.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vue2(标签-chrome|关键词-浏览器兼容)
  • ¥15 python网络流自动生成系统 医学领域
  • ¥15 sql查询仓库里都有什么
  • ¥15 代码的修改,添加和运行完善
  • ¥15 krpano-场景分组和自定义地图分组
  • ¥15 lammps Gpu加速出错
  • ¥15 关于PLUS模型中kapaa值的问题
  • ¥15 关于博途V17进行仿真时无法建立连接问题
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器