首页 文章

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

提问于
浏览
2

首先,我使用的是here中提到的代码 . 我按照某人的建议做了所有事情,我为我的应用生成了密钥并将其放入代码中 . 当我运行它时,我看到了巨大的json字符串,基本上就是那里的一切 . 在这里's a part of it to show you exactly what I'获得:

数组([0] => stdClass对象([created_at] =>星期四五月14日20:06:37 0000 2015 [id] => 5989425114966784000 [id_str] => 5989425114966784000 [text] =>这是我的第一条推文#hello [source] => Twitter Web客户端[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对象([id] = > 3064494912 [id_str] => 3064494912 [name] => hellomotoUserName [screen_name] => helloHQ [location] => [description] => [url] => [entities] => stdClass对象([description] => stdClass对象([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] =>贝尔格莱德[geo_enabled] => [已验证] => [statuses_count] => 3 [lang] => en [contributors_enabled] => [is_translator] => [ is_translation_enabled] => [profile_background_color] => C0DEED [profi le_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] => [贡献者] => [is_quote_status] => [转推帐号] => 0 [favorite_count] => 0 [实体] => stdClass对象([hashtags] =>数组([0] => stdClass对象([text] => hello [indices] =>数组([0] => 35 1 => 43))1 => stdClass对象([text] => hashtag [indices] => Array([0] => 45 1 => 53)))[symbols] => Array()[user_mentions] => Array()[urls] => Array())[favorited] = > [转发] => [lang] => en)1 => stdClass对象([created_at] =>星期二5月12日20:38:39 0000 2015 [id] => 598225796945847936 [id_str] => 598225796945847936 [text] = >这是我的第二条推文#hello [source] => 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 回答

  • 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>
    

相关问题