PHP+JSON+Twitter API
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP+JSON+Twitter API相关的知识,希望对你有一定的参考价值。
Nothing revolutionary, just a simple implementation.A reworking from Brian Cray's source code:
http://briancray.com/2009/08/21/tweeted-links-twitter-api-php-cache/
Note: requires creating a cache file, '/caches/twitter', relative to source.
<?php // define the parameters you want to use $username = 'richardmaisano'; $tweet_count = '10'; // define the api url $url = 'https://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $username . '&count=' . $count . '&include_entities=1&include_rts=1'; // define the cache file you created // check to see if the cache is older than five minutes // if so, refresh the data if ($cache_time > $five_minutes) { } else { } // set up the json return // the rest of the script just parses through the data, // namely the date and time of tweet and any 'entities' foreach ($json as $tweet) : $datetime = $tweet->created_at; $tweet_text = $tweet->text; // check if any entites exist and if so, replace then with hyperlinked versions foreach ($tweet->entities->urls as $url) { $find = $url->url; $replace = '<a href="'.$find.'">'.$find.'</a>'; } foreach ($tweet->entities->hashtags as $hashtag) { $find = '#'.$hashtag->text; $replace = '<a href="http://twitter.com/#!/search/%23'.$hashtag->text.'">'.$find.'</a>'; } foreach ($tweet->entities->user_mentions as $user_mention) { $find = "@".$user_mention->screen_name; $replace = '<a href="http://twitter.com/'.$user_mention->screen_name.'">'.$find.'</a>'; } } ?> <div class="entry tweet"> <span><?php echo $tweet_text; ?></span> <div class="meta"> <small><?php echo $time; ?>, <?php echo $date; ?></small> </div> </div> <?php endforeach; ?> <?php endif; ?>
以上是关于PHP+JSON+Twitter API的主要内容,如果未能解决你的问题,请参考以下文章
如何在 php 中创建类似 twitter 的 search.json
使用 Python 从 Twitter 流 API 中提取特定的 JSON 字段
Twitter API 在调用 statuses/update.json 时响应“您的凭据不允许访问此资源”