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+javascript实现二级级联菜单的制作
May 06 PHP
PHP中函数内引用全局变量的方法
Oct 20 PHP
简化php模板页面中分页代码的解析
Feb 06 PHP
收集的二十一个实用便利的PHP函数代码
Apr 22 PHP
PHP垃圾回收机制简单说明
Jul 22 PHP
fgetcvs在linux的问题
Jan 15 PHP
PHP关联链接常用代码
Nov 05 PHP
php使用codebase生成随机数
Mar 25 PHP
CodeIgniter输出中文乱码的两种解决办法
Jun 12 PHP
php文件上传后端处理小技巧
May 22 PHP
php检查函数必传参数是否存在的实例详解
Aug 28 PHP
PHP8.0新功能之Match表达式的使用
Jul 19 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操作csv文件代码实例汇总
2014/09/22 PHP
PHP使用Redis实现防止大并发下二次写入的方法
2017/10/09 PHP
浅谈php的TS和NTS的区别
2019/03/13 PHP
Jsonp 跨域的原理以及Jquery的解决方案
2011/06/27 Javascript
给事件响应函数传参数的四种方式小结
2013/12/05 Javascript
详解JavaScript中undefined与null的区别
2014/03/29 Javascript
js实现从数组里随机获取元素
2015/01/12 Javascript
JavaScript删除数组元素的方法
2015/03/20 Javascript
介绍JavaScript的一个微型模版
2015/06/24 Javascript
JS实现图片预加载之无序预加载功能代码
2017/05/12 Javascript
JavaScript中offsetWidth的bug及解决方法
2017/05/17 Javascript
详解Angular结合zTree异步加载节点数据
2018/01/20 Javascript
微信小程序实现人脸识别登陆的示例代码
2019/04/02 Javascript
jQuery实现简单飞机大战
2020/07/05 jQuery
vant 中van-list的用法说明
2020/11/11 Javascript
[39:07]LGD vs VP 2018国际邀请赛淘汰赛BO3 第二场 8.21
2018/08/22 DOTA
使用Python编写vim插件的简单示例
2015/04/17 Python
python中list常用操作实例详解
2015/06/03 Python
Python实现数据库编程方法详解
2015/06/09 Python
python使用wmi模块获取windows下的系统信息 监控系统
2015/10/27 Python
Python3实现并发检验代理池地址的方法
2016/09/18 Python
Python编程中NotImplementedError的使用方法
2018/04/21 Python
Python实现简单的文本相似度分析操作详解
2018/06/16 Python
Django开发中的日志输出的方法
2018/07/02 Python
html5 input元素新特性_动力节点Java学院整理
2017/07/06 HTML / CSS
自我评价是什么
2014/01/04 职场文书
开业庆典策划方案
2014/02/18 职场文书
网络编辑求职信
2014/04/30 职场文书
火灾现场处置方案
2014/05/28 职场文书
旅游安全责任协议书
2016/03/22 职场文书
创业计划书之农家乐
2019/10/09 职场文书
python数据库批量插入数据的实现(executemany的使用)
2021/04/30 Python
Python 类,对象,数据分类,函数参数传递详解
2021/09/25 Python
Spring Cloud 中@FeignClient注解中的contextId属性详解
2021/09/25 Java/Android
Nebula Graph解决风控业务实践
2022/03/31 MySQL
Python基本的内置数据类型及使用方法
2022/04/13 Python