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 相关文章推荐
粗略计算在线时间,bug:ip相同
Dec 09 PHP
sqlyog 中文乱码问题的设置方法
Oct 19 PHP
php 多线程上下文中安全写文件实现代码
Dec 28 PHP
php中防止SQL注入的最佳解决方法
Apr 25 PHP
基于empty函数的判断详解
Jun 17 PHP
2014过年倒计时示例
Jan 31 PHP
destoon之一键登录设置
Jun 21 PHP
实现在同一方法中获取当前方法中新赋值的session值解决方法
Jun 26 PHP
php通过function_exists检测函数是否存在的方法
Mar 18 PHP
学习php设计模式 php实现状态模式
Dec 07 PHP
PHP pthreads v3使用中的一些坑和注意点分析
Feb 21 PHP
浅谈Laravel中使用Slack进行异常通知
May 29 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仿QQ验证码的实例分析
2013/07/01 PHP
php站内搜索关键词变亮的实现方法
2014/12/30 PHP
初识laravel5
2015/03/02 PHP
PHP使用适合阅读的格式显示文件大小的方法
2015/03/05 PHP
分享3个php获取日历的函数
2015/09/25 PHP
PHP获取文件扩展名的4种方法
2015/11/24 PHP
Yii统计不同类型邮箱数量的方法
2016/10/18 PHP
DWZ+ThinkPHP开发时遇到的问题分析
2016/12/12 PHP
调用js时ie6和ie7,ff的区别
2009/08/19 Javascript
基于jquery的3d效果实现代码
2011/03/23 Javascript
jquery获取被勾选的checked(选中)的那一行的3列和4列的值
2013/07/04 Javascript
js中function()使用方法
2013/12/24 Javascript
Javascript动态创建div的方法
2015/02/09 Javascript
jQuery提示插件alertify使用指南
2015/04/21 Javascript
js实现精确到秒的倒计时效果
2016/05/29 Javascript
预防网页挂马的方法总结
2016/11/03 Javascript
JS实战篇之收缩菜单表单布局
2016/12/10 Javascript
Angular 输入框实现自定义验证功能
2017/02/19 Javascript
微信小程序分享海报生成的实现方法
2018/12/10 Javascript
一文快速详解前端框架 Vue 最强大的功能
2019/05/21 Javascript
[37:02]OG vs INfamous 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/17 DOTA
python 连接各类主流数据库的实例代码
2018/01/30 Python
python3+mysql查询数据并通过邮件群发excel附件
2018/02/24 Python
python集合比较(交集,并集,差集)方法详解
2018/09/13 Python
Python面向对象程序设计类变量与成员变量、类方法与成员方法用法分析
2019/04/12 Python
如何分离django中的媒体、静态文件和网页
2019/11/12 Python
Anaconda 查看、创建、管理和使用python环境的方法
2019/12/03 Python
Django中的模型类设计及展示示例详解
2020/05/29 Python
茵宝(Umbro)英国官方商店:英国足球服装生产商
2016/12/29 全球购物
Lookfantastic阿联酋官网:英国知名美妆护肤购物网站
2020/05/26 全球购物
英语自我介绍演讲稿
2014/09/01 职场文书
2014党的群众路线教育实践活动总结报告
2014/10/31 职场文书
关于感谢信的范文
2015/01/23 职场文书
重温入党誓词主持词
2015/06/29 职场文书
2016年中秋节晚会领导致辞
2015/11/26 职场文书
CentOS7安装MySQL8的超级详细教程(无坑!)
2022/06/10 Servers