【问题标题】:Parsing JSON / associative arrays in from Twitter API response in PHP从 PHP 中的 Twitter API 响应解析 JSON / 关联数组
【发布时间】:2013-08-20 12:57:05
【问题描述】:

我正在使用 Twitter API v.1.1 来检索与某些主题标签相关的推文。

$tweets = $connection->get("xxxxxxxxxxxxxxx");
$tweets = json_encode($tweets);

echo '<pre>';
    $objJson = json_decode($tweets, TRUE);
    print_r ($objJson);
    echo '</pre>';
?>

上面是我用来收集响应的代码。

这是我得到的回复

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

                    [created_at] => Sun Aug 18 16:24:36 +0000 2013
                    [id] => 369132733080936448
                    [id_str] => 369132733080936448
                    [text] => Macy's Almeda, Houston, TX:  Retail Commission Sales Associa - http://t.co/iz3YdBBCzC #jobs #Macy's #Houston
                    [source] => Best Jobs Online
                    [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] => 237507695
                            [id_str] => 237507695
                            [name] => Best Jobs
                            [screen_name] => bestjobsonline
                            [location] => 
                            [description] => Best Jobs Online - we search the web for the best internships and jobs for you. No need to search yourself! Follow our stream and boost your career!
                            [url] => http://t.co/SkmHCpOnzM
                            [entities] => Array
                                (
                                    [url] => Array
                                        (
                                            [urls] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [url] => http://t.co/SkmHCpOnzM
                                                            [expanded_url] => http://www.internships2013.com
                                                            [display_url] => internships2013.com
                                                            [indices] => Array
                                                                (
                                                                    [0] => 0
                                                                    [1] => 22
                                                                )

                                                        )

                                                )

                                        )

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

                                        )

                                )

                            [protected] => 
                            [followers_count] => 6172
                            [friends_count] => 13
                            [listed_count] => 240
                            [created_at] => Thu Jan 13 00:36:32 +0000 2011
                            [favourites_count] => 0
                            [utc_offset] => -18000
                            [time_zone] => Central Time (US & Canada)
                            [geo_enabled] => 
                            [verified] => 
                            [statuses_count] => 847813
                            [lang] => en
                            [contributors_enabled] => 
                            [is_translator] => 
                            [profile_background_color] => 022330
                            [profile_background_image_url] => http://a0.twimg.com/images/themes/theme15/bg.png
                            [profile_background_image_url_https] => https://si0.twimg.com/images/themes/theme15/bg.png
                            [profile_background_tile] => 
                            [profile_image_url] => http://a0.twimg.com/profile_images/1214148827/bjo_logo_normal.png
                            [profile_image_url_https] => https://si0.twimg.com/profile_images/1214148827/bjo_logo_normal.png
                            [profile_link_color] => 0084B4
                            [profile_sidebar_border_color] => A8C7F7
                            [profile_sidebar_fill_color] => C0DFEC
                            [profile_text_color] => 333333
                            [profile_use_background_image] => 1
                            [default_profile] => 
                            [default_profile_image] => 
                            [following] => 
                            [follow_request_sent] => 
                            [notifications] => 
                        )

                    [geo] => 
                    [coordinates] => 
                    [place] => 
                    [contributors] => 
                    [retweet_count] => 0
                    [favorite_count] => 0
                    [entities] => Array
                        (
                            [hashtags] => Array
                                (
                                    [0] => Array
                                        (
                                            [text] => jobs
                                            [indices] => Array
                                                (
                                                    [0] => 86
                                                    [1] => 91
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [text] => Macy
                                            [indices] => Array
                                                (
                                                    [0] => 92
                                                    [1] => 97
                                                )

                                        )

                                    [2] => Array
                                        (
                                            [text] => Houston
                                            [indices] => Array
                                                (
                                                    [0] => 100
                                                    [1] => 108
                                                )

                                        )

                                )

                            [symbols] => Array
                                (
                                )

                            [urls] => Array
                                (
                                    [0] => Array
                                        (
                                            [url] => http://t.co/iz3YdBBCzC
                                            [expanded_url] => http://bit.ly/16UtroW
                                            [display_url] => bit.ly/16UtroW
                                            [indices] => Array
                                                (
                                                    [0] => 63
                                                    [1] => 85
                                                )

                                        )

                                )

                            [user_mentions] => Array
                                (
                                )

                        )

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

如何格式化此回复以仅显示这些推文的日期和文本。谢谢您的帮助。我是菜鸟,任何帮助都会派上用场!

【问题讨论】:

    标签: php json twitter twitter-oauth associative-array


    【解决方案1】:

    这会遍历您的状态并将 created_at 和 text 的值添加到新数组中。

    $data = array();
    foreach($objJson["statuses"] as $status) {
        $data[] = array(
            "created_at" => $status["created_at"],
            "text" => $status["text"]
        );
    }
    echo "<pre>" . print_r($data,1) . "</pre>";
    

    【讨论】:

    • 您好,感谢您的帮助。我尝试了您的代码,现在收到以下错误消息:“尝试获取非对象的属性”和“为 foreach() 提供的参数无效”这是我目前的代码:'$tweets = $connection->get("@987654321 @); $tweets = json_encode($tweets); $obJson = json_decode($tweets, TRUE); $data = array(); foreach($obJson->statuses as $status) { $data[] = array(" created_at" => $status->createdAt, "text" => $status->text ); } echo "
      " . print_r($data,1) . "
      ";'
    • 我不久前更改了我的代码,因为我第一次在你的 json_decode 中没有看到 TRUE,这意味着你不想要一个对象,而是一个关联数组。所以尝试更新的代码!
    猜你喜欢
    • 2019-03-01
    • 2012-01-19
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    相关资源
    最近更新 更多