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.
  1. <?php
  2. // define the parameters you want to use
  3. $username = 'richardmaisano';
  4. $tweet_count = '10';
  5.  
  6. // define the api url
  7. $url = 'https://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $username . '&count=' . $count . '&include_entities=1&include_rts=1';
  8.  
  9. // define the cache file you created
  10. $cache = dirname(__FILE__) . '/caches/twitter';
  11.  
  12. // check to see if the cache is older than five minutes
  13. // if so, refresh the data
  14. $cache_time = filemtime($cache);
  15. $five_minutes = time() - 300;
  16.  
  17. if ($cache_time > $five_minutes)
  18. {
  19. $data = file_get_contents($cache);
  20. }
  21. else
  22. {
  23. $data = file_get_contents($url);
  24. $cachefile = fopen($cache, 'wb');
  25. fwrite($cachefile, $data);
  26. fclose($cachefile);
  27. }
  28.  
  29. // set up the json return
  30. $json = json_decode($data);
  31.  
  32. // the rest of the script just parses through the data,
  33. // namely the date and time of tweet and any 'entities'
  34. if (!empty($json)) :
  35. foreach ($json as $tweet) :
  36. $datetime = $tweet->created_at;
  37. $date = date('M d, Y', strtotime($datetime));
  38. $time = date('g:ia', strtotime($datetime));
  39. $tweet_text = $tweet->text;
  40.  
  41. // check if any entites exist and if so, replace then with hyperlinked versions
  42. if (!empty($tweet->entities->urls) || !empty($tweet->entities->hashtags) || !empty($tweet->entities->user_mentions)) {
  43. foreach ($tweet->entities->urls as $url) {
  44. $find = $url->url;
  45. $replace = '<a href="'.$find.'">'.$find.'</a>';
  46. $tweet_text = str_replace($find,$replace,$tweet_text);
  47. }
  48.  
  49. foreach ($tweet->entities->hashtags as $hashtag) {
  50. $find = '#'.$hashtag->text;
  51. $replace = '<a href="http://twitter.com/#!/search/%23'.$hashtag->text.'">'.$find.'</a>';
  52. $tweet_text = str_replace($find,$replace,$tweet_text);
  53. }
  54.  
  55. foreach ($tweet->entities->user_mentions as $user_mention) {
  56. $find = "@".$user_mention->screen_name;
  57. $replace = '<a href="http://twitter.com/'.$user_mention->screen_name.'">'.$find.'</a>';
  58. $tweet_text = str_ireplace($find,$replace,$tweet_text);
  59. }
  60. }
  61. ?>
  62.  
  63. <div class="entry tweet">
  64. <span><?php echo $tweet_text; ?></span>
  65. <div class="meta">
  66. <small><?php echo $time; ?>, <?php echo $date; ?></small>
  67. </div>
  68. </div>
  69.  
  70. <?php endforeach; ?>
  71. <?php endif; ?>

以上是关于PHP+JSON+Twitter API的主要内容,如果未能解决你的问题,请参考以下文章

如何在 php 中创建类似 twitter 的 search.json

使用 Python 从 Twitter 流 API 中提取特定的 JSON 字段

Android 开发中的 Twitter JSON API

Twitter API 在调用 statuses/update.json 时响应“您的凭据不允许访问此资源”

无法使用 Twitter API、PHP 和 curl 发布图像和文本?媒体/上传和状态/更新

处理 PHP JSON 对象中的数据