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

穆罕默德

我有一个带有twitter搜索api的twitter php搜索脚本。

我的代码在这里:

<?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>";
?>

它返回给我一个带有json的示例搜索结果。让我给出一个示例结果:

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: [email protected]
                                    [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] => [email protected]
                                    [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
                )..........

我的搜索JSON结果的一部分在上面。

现在,我只想从此结果获得推文([文本])。

如何使用JSON从php获取所有[text]值?

穆罕默德

首先,我更改了问题中的部分代码。因为在引用json API时是错误的。

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

这部分被这个改变了

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

然后,需要一个循环来从json数组中获取所有推文。解决方案:

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

}

它对我有效。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

用户的Twitter API高级搜索推文

来自分类Dev

Android - 如何从 Twitter API 获取某人的推文

来自分类Dev

当有人使用Twitter API和PHP发布推文时如何获取通知

来自分类Dev

twitter API搜索/推文-如何使用查询运算符过滤搜索与用户相关的推文?

来自分类Dev

使用Twitter搜索API仅获取带有图片的推文

来自分类Dev

Twitter获取搜索推文API不适用于哈希标签

来自分类Dev

仅获取包含图像的推文(使用 Python 的 Twitter 搜索 API)

来自分类Dev

NodeJS Twitter API搜索超过100条推文

来自分类Dev

Twitter搜索API不返回任何推文?

来自分类Dev

Twitter搜索API不返回任何推文?

来自分类Dev

NodeJS Twitter API搜索超过100条推文

来自分类Dev

Twitter API - Python,按日期搜索推文

来自分类Dev

Twitter API:如何根据查询词和预定时间跨度和推文特征搜索推文

来自分类Dev

如何使用 Twitter API 收藏推文

来自分类Dev

Twitter4j 获取推文的 JSON 格式

来自分类Dev

如何使用PHP Twitter API转推?

来自分类Dev

如何获取特定语言的Twitter热门推文?

来自分类Dev

在API v1.1中使用JSON从Twitter上获取推文

来自分类Dev

Twitter搜索推文API-不同的用户但返回相同的推文

来自分类Dev

如何使用Spring Social和Twitter Streaming API获取基于位置的推文?

来自分类Dev

用户使用Twitter API如何获取最近三个月以来的推文

来自分类Dev

给定Twitter API Rate Lmit,如何获取至少10,000条推文?

来自分类Dev

如何通过 tweepy 使用 Twitter 搜索 API 执行布尔搜索

来自分类Dev

Twitter API-获取具有特定ID的推文

来自分类Dev

Twitter API仅获取用户推文

来自分类Dev

Twitter API 1.1从多个用户处获取推文

来自分类Dev

如何在 Twitter API 搜索中获取全文

来自分类Dev

使用Python提取Twitter API数据-计算推文

来自分类Dev

从 Twitter API 获取所有推文和重新推文

Related 相关文章

  1. 1

    用户的Twitter API高级搜索推文

  2. 2

    Android - 如何从 Twitter API 获取某人的推文

  3. 3

    当有人使用Twitter API和PHP发布推文时如何获取通知

  4. 4

    twitter API搜索/推文-如何使用查询运算符过滤搜索与用户相关的推文?

  5. 5

    使用Twitter搜索API仅获取带有图片的推文

  6. 6

    Twitter获取搜索推文API不适用于哈希标签

  7. 7

    仅获取包含图像的推文(使用 Python 的 Twitter 搜索 API)

  8. 8

    NodeJS Twitter API搜索超过100条推文

  9. 9

    Twitter搜索API不返回任何推文?

  10. 10

    Twitter搜索API不返回任何推文?

  11. 11

    NodeJS Twitter API搜索超过100条推文

  12. 12

    Twitter API - Python,按日期搜索推文

  13. 13

    Twitter API:如何根据查询词和预定时间跨度和推文特征搜索推文

  14. 14

    如何使用 Twitter API 收藏推文

  15. 15

    Twitter4j 获取推文的 JSON 格式

  16. 16

    如何使用PHP Twitter API转推?

  17. 17

    如何获取特定语言的Twitter热门推文?

  18. 18

    在API v1.1中使用JSON从Twitter上获取推文

  19. 19

    Twitter搜索推文API-不同的用户但返回相同的推文

  20. 20

    如何使用Spring Social和Twitter Streaming API获取基于位置的推文?

  21. 21

    用户使用Twitter API如何获取最近三个月以来的推文

  22. 22

    给定Twitter API Rate Lmit,如何获取至少10,000条推文?

  23. 23

    如何通过 tweepy 使用 Twitter 搜索 API 执行布尔搜索

  24. 24

    Twitter API-获取具有特定ID的推文

  25. 25

    Twitter API仅获取用户推文

  26. 26

    Twitter API 1.1从多个用户处获取推文

  27. 27

    如何在 Twitter API 搜索中获取全文

  28. 28

    使用Python提取Twitter API数据-计算推文

  29. 29

    从 Twitter API 获取所有推文和重新推文

热门标签

归档