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 相关文章推荐
web方式ftp
Oct 09 PHP
php下图片文字混合水印与缩略图实现代码
Dec 11 PHP
在命令行下运行PHP脚本[带参数]的方法
Jan 22 PHP
让的PHP代码飞起来的40条小技巧(提升php效率)
Apr 12 PHP
PHP获取数组最后一个值的2种方法
Jan 21 PHP
php实现RSA加密类实例
Mar 26 PHP
YII CLinkPager分页类扩展增加显示共多少页
Jan 29 PHP
PHP获取用户访问IP地址的5种方法
May 16 PHP
php中static 静态变量和普通变量的区别
Dec 01 PHP
浅谈php中curl、fsockopen的应用
Dec 10 PHP
laravel配置Redis多个库的实现方法
Apr 10 PHP
Laravel框架中缓存的使用方法分析
Sep 06 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
对象失去焦点时自己动提交数据的实现代码
2012/11/06 PHP
destoon切换城市后实现logo旁边显示地区名称的方法
2014/08/21 PHP
如何解决PHP使用mysql_query查询超大结果集超内存问题
2016/03/14 PHP
javascript 解析后的xml对象的读取方法细解
2009/07/25 Javascript
js阻止默认事件与js阻止事件冒泡示例分享 js阻止冒泡事件
2014/01/27 Javascript
jquery下拉select控件操作方法分享(jquery操作select)
2014/03/25 Javascript
javascript常见用法总结
2014/05/22 Javascript
jQuery表格插件datatables用法总结
2014/09/05 Javascript
JavaScript中window.showModalDialog()用法详解
2014/12/18 Javascript
jquery实现将获取的颜色值转换为十六进制形式的方法
2014/12/20 Javascript
详解JavaScript正则表达式之RegExp对象
2015/12/13 Javascript
浅谈js常用内置方法和对象
2016/09/24 Javascript
JS日程管理插件FullCalendar简单实例
2017/02/07 Javascript
Vue.js仿Metronic高级表格(一)静态设计
2017/04/17 Javascript
angular select 默认值设置方法
2017/06/23 Javascript
vue2.0的contextmenu右键弹出菜单的实例代码
2017/07/24 Javascript
vue组件生命周期详解
2017/11/07 Javascript
vue实现的封装全局filter并统一管理操作示例
2020/02/02 Javascript
vue fetch中的.then()的正确使用方法
2020/04/17 Javascript
javascript+Canvas实现画板功能
2020/06/23 Javascript
解决vue-router 切换tab标签关闭时缓存问题
2020/07/22 Javascript
vue实现几秒后跳转新页面代码
2020/09/09 Javascript
JS实现4位随机验证码
2020/10/19 Javascript
python 自动重连wifi windows的方法
2018/12/18 Python
Python操作rabbitMQ的示例代码
2019/03/19 Python
python3射线法判断点是否在多边形内
2019/06/28 Python
selenium+PhantomJS爬取豆瓣读书
2019/08/26 Python
python实现四人制扑克牌游戏
2020/04/22 Python
Python函数调用追踪实现代码
2020/11/27 Python
css3截图_动力节点Java学院整理
2017/07/11 HTML / CSS
Seavenger官网:潜水服、浮潜、靴子和袜子
2020/03/05 全球购物
法定代表人身份证明书
2014/09/10 职场文书
英语通知范文
2015/04/22 职场文书
工程项目合作意向书
2015/05/08 职场文书
导游词之河北滦平金山岭长城
2019/10/16 职场文书
Win10 Anaconda安装python-pcl
2022/04/29 Servers