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的两种交互方式
Oct 25 PHP
PHP中的strtr函数使用介绍(str_replace)
Oct 20 PHP
解析PHP自带的进位制之间的转换函数
Jun 08 PHP
php实现的漂亮分页方法
Apr 17 PHP
php基于表单密码验证与HTTP验证用法实例
Jan 06 PHP
PHP中的类型约束介绍
May 11 PHP
PHP检测用户语言的方法
Jun 15 PHP
详解php比较操作符的安全问题
Dec 03 PHP
php 实现Hash表功能实例详解
Nov 29 PHP
微信封装的调用微信签名包的类库
Jun 08 PHP
Laravel 模型关联基础教程详解
Sep 17 PHP
php计数排序算法的实现代码(附四个实例代码)
Mar 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 fread函数使用方法总结
2019/05/28 PHP
用JavaScript实现动画效果的方法
2013/07/20 Javascript
javascript实现颜色渐变的方法
2013/10/30 Javascript
Js操作树节点自动折叠展开的几种方法
2014/05/05 Javascript
javascript框架设计读书笔记之字符串的扩展和修复
2014/12/02 Javascript
简单实现限制uploadify上传个数
2015/11/16 Javascript
简单的js表格操作
2016/09/24 Javascript
原生JS实现圣旨卷轴展开效果
2017/03/06 Javascript
AngularJS实现动态添加Option的方法
2017/05/17 Javascript
js实现鼠标单击Tab表单切换效果
2018/05/16 Javascript
如何封装了一个vue移动端下拉加载下一页数据的组件
2019/01/06 Javascript
利用JavaScript将Excel转换为JSON示例代码
2019/06/14 Javascript
微信小程序实现点击效果
2019/06/21 Javascript
Vue.js递归组件实现组织架构树和选人功能案例分析
2019/07/03 Javascript
Vue页面切换和a链接的本质区别详解
2019/11/12 Javascript
JS eval代码快速解密实例解析
2020/04/23 Javascript
如何基于jQuery实现五角星评分
2020/09/02 jQuery
Openlayers绘制聚合标注
2020/09/28 Javascript
jquery实现异步文件上传ajaxfileupload.js
2020/10/23 jQuery
Python如何判断数独是否合法
2016/09/08 Python
利用python画一颗心的方法示例
2017/01/31 Python
PyQt5每天必学之工具提示功能
2018/04/19 Python
Python解析并读取PDF文件内容的方法
2018/05/08 Python
python实现人民币大写转换
2018/06/20 Python
Python将字符串常量转化为变量方法总结
2019/03/17 Python
Python namedtuple命名元组实现过程解析
2020/01/08 Python
解决pycharm debug时界面下方不出现step等按钮及变量值的问题
2020/06/09 Python
python不同系统中打开方法
2020/06/23 Python
如何用python批量调整视频声音
2020/12/22 Python
杭州联环马网络笔试题面试题
2013/08/04 面试题
linux面试相关问题
2012/08/11 面试题
卫生安全检查制度
2014/02/04 职场文书
食品委托检验协议书范本
2014/09/12 职场文书
教育读书笔记
2015/07/02 职场文书
2016元旦主持人经典开场白台词
2015/12/03 职场文书
MySQL数据库查询进阶之多表查询详解
2022/04/08 MySQL