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

时间:2015-07-12 20:27:10

标签: php html json twitter

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

  

数组([0] => stdClass对象([created_at] => Thu May 14 20:06:37   +0000 2015 [id] => 5989425114966784000 [id_str] => 5989425114966784000 [text] =>这是我的第一条推文#hello   [来源] => Twitter Web客户端[截断] => [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对象([id] => 3064494912 [id_str] => 3064494912 [name] =>   hellomotoUserName [screen_name] => helloHQ [location] => [描述] =>   [url] => [entities] => stdClass对象([description] => stdClass   Object([urls] => Array()))[protected] => [followers_count] => 0   [friends_count] => 0 [listed_count] => 0 [created_at] => 2月27日星期五   2015年17:39:51 +0000 [favourites_count] => 0 [utc_offset] => 7200   [time_zone] =>贝尔格莱德[geo_enabled] => [已验证] =>   [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] => [follow] =>   [follow_request_sent] => [notifications] => )[geo] => [坐标]   => [place] => [贡献者] => [is_quote_status] => [retweet_count] => 0 [favorite_count] => 0 [实体] => stdClass对象([hashtags] =>数组([0] => stdClass对象([text] => hello [indices] =>数组([0] => 35 1 => 43))1 => stdClass对象([text] =>   hashtag [indices] =>数组([0] => 45 1 => 53)))[符号] =>   Array()[user_mentions] => Array()[urls] =>数组())   [favorited] => [转推] => [lang] => en)1 => stdClass对象(   [created_at] => 5月12日星期二20:38:39 +0000 2015 [id] =>   598225796945847936 [id_str] => 598225796945847936 [text] =>这是我的第二条推文#hello   [来源] => Twitter Web客户端[截断] => [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条推文及其日期和时间..非常感谢

1 个答案:

答案 0 :(得分:1)

您可以使用此代码($ 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>