PHP封装的Twitter访问类实例
时间:2022-01-21 00:02:45|来源:网络精心整理
这篇文章主要讲解的是PHP封装的Twitter访问类实例,文章内容非常详细,相信一定可以解决你的问题,需要的朋友可以参考下哦
本文实例讲述了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封装的Twitter访问类实例
本文地址:https://www.tabangni.com/phpstudy/5371.html
查看更多与“PHPTwitter”有关的文章
- 01-17PHP中基本符号及使用方法
- 01-19PHP count_chars()函数讲解
- 01-17PHP hex2bin()函数用法讲解
- 01-16PHP implode()函数用法讲解
- 01-20PHP lcfirst()函数定义与用法
- 01-20PHP join()函数用法与实例讲解
- 01-19PHP中quotemeta()函数的用法讲解
- 01-18PHP中number_format()函数的用法讲解
- 01-18PHP中str_split()函数的用法讲解
- 01-17一个正则的写法 php
- 01-16php 正则表达式学习笔记
- 01-17php下常用表单验证的正则表达式
- 01-20PHP 正则表达式分析RSS
- 01-16PHP 正则表达式验证中文的问题
- 01-18php中utf-8编码下用正则表达式如何匹配汉字
- 01-16PHP正则匹配图片并给图片加链接详解
- 01-17PHP 正则表达式的几则使用技巧
- 01-18PHP 正则表达式常用函数使用小结
- 01-18php 正则 不包含某字符串的正则表达式
- 01-18PHP 正则 email语句详解
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
推荐阅读
最近更新
- 01-21PHP封装的Twitter访问类实例
- 01-21顶级人才传授解决使用bootstrap的dropdown部件时报错:error:Bootstrap dropdown require Popper.js问题
- 01-21技术员阐明[Bootstrap-插件使用]Jcrop+fileinput组合实现头像上传功能实例代码
- 01-21MySQL与Oracle的语法区别详细对比
- 01-21技术员指导CSS教程:使用ul进行网页的多列布局
- 01-21大神指导css控制div中元素居中的示例
- 01-21大神阐明用函数式编程技术编写优美的 JavaScript_ibm
- 01-21权威技术员教您javascript结合Cookies实现浏览记录历史
- 01-21专业技术员说明JavaScript 验证浏览器是否支持javascript的方法小结
- 01-21MySQL 5.7.16 修改密码提示 ERROR 1054 (42S22): Unknown column ''password'' in ''field list''的原因