PHP封装的Twitter访问类实例


Posted in PHP onJuly 18, 2015

本文实例讲述了PHP封装的Twitter访问类。分享给大家供大家参考。具体如下:

class Twitter {
 /**
  * Method to make twitter api call for the users timeline in XML
  *
  * @access private
  * @param $twitter_id, $num_of_tweets
  * @return $xml
  */
 private function api_call($twitter_id, $num_of_tweets) {
  $c = curl_init();
  curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$twitter_id.xml?count=$num_of_tweets");
  curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 3);
  curl_setopt($c, CURLOPT_TIMEOUT, 5);
  $response  = curl_exec($c);
  $response_info = curl_getinfo($c);
  curl_close($c);
  if (intval($response_info['http_code']) == 200) {
   $xml = new SimpleXMLElement($response);
   return $xml;
  } else {
   return false;
  }
 }
 /**
  * Method to add hyperlink html tags to any urls, twitter ids or hashtags in tweet
  *
  * @access private
  * @param $text
  * @return $text
  */
 private function process_links($text) {
  $text = utf8_decode($text);
  $text = preg_replace('@(https?://([-\w\.]+)+(d+)?(/([\w/_\.]*(\?\S+)?)?)?)@', '<a href="$1">$1</a>', $text);
  $text = preg_replace("#(^|[\n ])@([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://www.twitter.com/\\2\" >@\\2</a>'", $text);
  $text = preg_replace("#(^|[\n ])\#([^ \"\t\n\r<]*)#ise", "'\\1<a href=\"http://hashtags.org/search?query=\\2\" >#\\2</a>'", $text);
  return $text;
 }
 /**
  * Main method to retrieve the tweets and return html for display
  *
  * @access public
  * @param $twitter_id, $num_of_tweets, $timezone
  * @return $result
  */
 public function get_tweets($twitter_id, $num_of_tweets = 3, $timezone = "America/Denver") {
  $include_replies = false;
  date_default_timezone_set($timezone);
  // the html markup
  $cont_o  = "<div id=\"tweets\">\n";
  $tweet_o = "<div class=\"status\">\n";
  $tweet_c = "</div>\n\n";
  $detail_o = "<div class=\"details\">\n";
  $detail_c = "</div>\n\n";
  $cont_c  = "</div>\n";
  if ($twitter_xml = $this->api_call($twitter_id, $num_of_tweets)) {
   $result  = $cont_o;
   foreach ($twitter_xml->status as $key => $status) {
    if ($include_replies == true | substr_count($status->text, "@") == 0 | strpos($status->text, "@") != 0) {
     $tweet = $this->process_links($status->text);
     $result .= $tweet_o . $tweet . $tweet_c . $detail_o . date('D jS M y H:i', strtotime($status->created_at)) . $detail_c;
    }
   }
   $result  .= $cont_c;
  } else {
   $result  .= $cont_o . $tweet_o . "Twitter seems to be unavailable at the moment." . $tweet_c . $cont_c;
  }
  return $result;
 }
}

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
PHP中10个不常见却非常有用的函数
Mar 21 PHP
php设计模式 Bridge (桥接模式)
Jun 26 PHP
PHP常用开发函数解析之数组篇[未完结]
Jul 30 PHP
用PHP实现浏览器点击下载TXT文档的方法详解
Jun 02 PHP
php构造函数实例讲解
Nov 13 PHP
php根据日期判断星座的函数分享
Feb 13 PHP
PHP中使用匿名函数操作数据库的例子
Nov 17 PHP
yii2实现分页,带搜索的分页功能示例
Jan 07 PHP
深入解析PHP中SESSION反序列化机制
Mar 01 PHP
php-fpm开启状态统计的方法详解
Jun 23 PHP
浅谈laravel中的关联查询with的问题
Oct 10 PHP
基于php伪静态的实现方法解析
Jul 31 PHP
PHP+Javascript实现在线拍照功能实例
Jul 18 #PHP
thinkphp autoload 命名空间自定义 namespace
Jul 17 #PHP
简单谈谈PHP vs Node.js
Jul 17 #PHP
php+html5基于websocket实现聊天室的方法
Jul 17 #PHP
php获取错误信息的方法
Jul 17 #PHP
PHP实现C#山寨ArrayList的方法
Jul 16 #PHP
PHP计算加权平均数的方法
Jul 16 #PHP
You might like
php下使用无限生命期Session的方法
2007/03/16 PHP
一步一步学习PHP(6) 面向对象
2010/02/16 PHP
php 日期和时间的处理-郑阿奇(续)
2011/07/04 PHP
php 伪造本地文件包含漏洞的代码
2011/11/03 PHP
PHP自带方法验证邮箱是否存在
2016/02/01 PHP
PHP快速推送微信模板消息
2017/04/14 PHP
thinkphp5 加载静态资源路径与常量的方法
2017/12/24 PHP
javascript 一些用法小结
2009/09/11 Javascript
javascript拓展DOM操作 prependChild insertAfert
2010/11/17 Javascript
js+JQuery返回顶部功能如何实现
2012/12/03 Javascript
node在两个div之间移动,用ztree实现
2013/03/06 Javascript
jquery 实现上下滚动效果示例代码
2013/08/09 Javascript
PHP使用方法重载实现动态创建属性的get和set方法
2014/11/17 Javascript
angularJS与bootstrap结合实现动态加载弹出提示内容
2015/10/16 Javascript
JavaScript性能优化之小知识总结
2015/11/20 Javascript
详解AngularJS控制器的使用
2016/03/09 Javascript
JavaScript必看小技巧(必看)
2016/06/07 Javascript
javascript 跨域问题以及解决办法
2017/07/17 Javascript
H5+C3+JS实现双人对战五子棋游戏(UI篇)
2020/05/28 Javascript
python目录与文件名操作例子
2016/08/28 Python
pip命令无法使用的解决方法
2018/06/12 Python
Python3按一定数据位数格式处理bin文件的方法
2019/01/24 Python
python3+PyQt5 数据库编程--增删改实例
2019/06/17 Python
python实现按行分割文件
2019/07/22 Python
Pandas透视表(pivot_table)详解
2019/07/22 Python
python 实现两个npy档案合并
2020/07/01 Python
Python3 ffmpeg视频转换工具使用方法解析
2020/08/10 Python
天猫精选:上天猫,就够了
2016/09/21 全球购物
英国床和浴室商场:Bed & Bath Emporium
2018/05/20 全球购物
介绍一下HTTP、HTTPS和SSL
2012/12/16 面试题
十八届三中全会宣传方案
2014/02/21 职场文书
社区文化建设方案
2014/05/02 职场文书
贷款承诺书
2015/01/20 职场文书
英文感谢信范文
2015/01/21 职场文书
2015年感恩父亲节演讲稿
2015/03/19 职场文书
基于Golang 高并发问题的解决方案
2021/05/08 Golang