如何解析 Twitter api 返回的 json 数据并在我的网页上使用它?

How can I parse the json data returned by Twitter api and use it on my webpage?

首先,我正在使用提到的代码 here。我按照某人的建议做了所有事情,我生成了我的应用程序的密钥并将其放入代码中。当我 运行 它时,我看到了巨大的 json 字符串,基本上所有的东西都在那里。下面是它的一部分,向您展示我得到的结果:

Array ( [0] => stdClass Object ( [created_at] => Thu May 14 20:06:37 +0000 2015 [id] => 5989425114966784000 [id_str] => 5989425114966784000 [text] => This is my first tweet #hello [source] => Twitter Web Client [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] => stdClass Object ( [id] => 3064494912 [id_str] => 3064494912 [name] => hellomotoUserName [screen_name] => helloHQ [location] => [description] => [url] => [entities] => stdClass Object ( [description] => stdClass Object ( [urls] => Array ( ) ) ) [protected] => [followers_count] => 0 [friends_count] => 0 [listed_count] => 0 [created_at] => Fri Feb 27 17:39:51 +0000 2015 [favourites_count] => 0 [utc_offset] => 7200 [time_zone] => Belgrade [geo_enabled] => [verified] => [statuses_count] => 3 [lang] => en [contributors_enabled] => [is_translator] => [is_translation_enabled] => [profile_background_color] => C0DEED [profile_background_image_url] => http://abs.twimg.com/images/themes/theme1/bg.png [profile_background_image_url_https] => https://abs.twimg.com/images/themes/theme1/bg.png [profile_background_tile] => [profile_image_url] => http://pbs.twimg.com/profile_images/5975166356332425/fXmL-aI__normal.png [profile_image_url_https] => https://pbs.twimg.com/profile_images/5975166356332425/fXmL-aI__normal.png [profile_link_color] => 0084B4 [profile_sidebar_border_color] => C0DEED [profile_sidebar_fill_color] => DDEEF6 [profile_text_color] => 333333 [profile_use_background_image] => 1 [has_extended_profile] => [default_profile] => 1 [default_profile_image] => [following] => [follow_request_sent] => [notifications] => ) [geo] => [coordinates] => [place] => [contributors] => [is_quote_status] => [retweet_count] => 0 [favorite_count] => 0 [entities] => stdClass Object ( [hashtags] => Array ( [0] => stdClass Object ( [text] => hello [indices] => Array ( [0] => 35 1 => 43 ) ) 1 => stdClass Object ( [text] => hashtag [indices] => Array ( [0] => 45 1 => 53 ) ) ) [symbols] => Array ( ) [user_mentions] => Array ( ) [urls] => Array ( ) ) [favorited] => [retweeted] => [lang] => en ) 1 => stdClass Object ( [created_at] => Tue May 12 20:38:39 +0000 2015 [id] => 598225796945847936 [id_str] => 598225796945847936 [text] => This is my second tweet #hello [source] => Twitter Web Client [truncated] => [in_reply_to_status_id] => [in_reply_to_status_id_str] => [in_reply_to_user_id] => [in_reply_to_user_id_str] => ...

等等

另一方面 - 这是我的网页,上面有这个 Twitter 容器:

<section class="twitter-container">
            <div class="twitter">
                <ul class="tweet_list" id="tweet_list">
                    <li class="tweet_first tweet_even">
                        <span class="tweet_text">I want to put here the text from my latest tweets</span><span class="tweet_time"><a href="http://twitter.com/mytwitteraccount" title="view tweet on twitter">date of first tweet</a></span>
                    </li>
                    <li class="tweet_even">
                        <span class="tweet_text">Again, the same as above, how can I put here latest #tweet?
                            <a href="#">#myFirstTweet</a></span><span class="tweet_time"><a href="http://twitter.com/mytwitteraccount" title="view tweet on twitter">date of 2nd tweet</a></span>
                    </li>

                </ul>
            </div>
        </section>

现在 - 你能帮我解析 json 并将结果拟合到我的 html 代码中吗?我想放在那里,例如最后 5 条推文及其日期和时间。非常感谢

您可以使用此代码($tweets 变量是包含所有推文的数组):

<section class="twitter-container">
    <div class="twitter">
        <ul class="tweet_list" id="tweet_list">
            <?php $t = 0; ?>
            <?php foreach ($tweets as $tweet) { ?>
                <?php $t++; ?>
                <?php if ($t>5) break; ?>
                <li class="<?php if ($t==1) print 'tweet-first'; ?> tweet-<?php print $t; ?> tweet_even">
                    <span class="tweet_text"><?php print $tweet->text; ?></span><span class="tweet_time"><a href="http://twitter.com/mytwitteraccount" title="view tweet on twitter"><?php print $tweet->created_at; ?></a></span>
                </li>
            <?php } ?>
        </ul>
    </div>
</section>