PHP封裝的Twitter訪問類實例
更新時間:2015年07月18日 10:50:48 作者:鑒客
這篇文章主要介紹了PHP封裝的Twitter訪問類,通過curl調(diào)用實現(xiàn)針對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程序設(shè)計有所幫助。
您可能感興趣的文章:
- PHP面向?qū)ο笕筇攸c學(xué)習(xí)(充分理解抽象、封裝、繼承、多態(tài))
- PHP實現(xiàn)的封裝驗證碼類詳解
- php遞歸獲取目錄內(nèi)文件(包含子目錄)封裝類分享
- PHP類繼承 extends使用介紹
- PHP快速按行讀取CSV大文件的封裝類分享(也適用于其它超大文本文件)
- php實現(xiàn)mysql封裝類示例
- PHP封裝的一個支持HTML、JS、PHP重定向的多功能跳轉(zhuǎn)函數(shù)
- 解密ThinkPHP3.1.2版本之模板繼承
- php的curl封裝類用法實例
- thinkphp模板繼承實例簡述
- php繼承中方法重載(覆蓋)的應(yīng)用場合
- php構(gòu)造函數(shù)的繼承方法
- PHP封裝的HttpClient類用法實例
- php類的擴(kuò)展和繼承用法實例
- php類的定義與繼承用法實例
- PHP封裝CURL擴(kuò)展類實例
- php圖片水印添加、壓縮、剪切的封裝類實現(xiàn)
- php實現(xiàn)的Curl封裝類Curl.class.php用法實例分析
- PHP類的封裝與繼承詳解
相關(guān)文章
PHP實時統(tǒng)計中文字?jǐn)?shù)和區(qū)別
今天小編就為大家分享一篇關(guān)于PHP統(tǒng)計實時統(tǒng)計漢字個數(shù)和區(qū)別,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-02-02php自定義函數(shù)實現(xiàn)漢字轉(zhuǎn)換utf8編碼的方法
這篇文章主要介紹了php自定義函數(shù)實現(xiàn)漢字轉(zhuǎn)換utf8編碼的方法,涉及php針對字符串的遍歷、截取及編碼轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2016-09-09PHP以指定字段為索引返回數(shù)據(jù)庫所取的數(shù)據(jù)數(shù)組
本文與大家分享幾個使用得PHP編程技巧,有些技巧是在看別人代碼的時候?qū)W來的,有些是自己總結(jié)的,下面為大家介紹下以特定字段為索引,返回數(shù)據(jù)庫取的數(shù)據(jù)數(shù)組,感興趣的朋友可以了解下哈2013-06-06php將print_r處理后的數(shù)據(jù)還原為原始數(shù)組的解決方法
下面小編就為大家?guī)硪黄猵hp中將print_r處理后的數(shù)據(jù)還原為原始數(shù)組的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考2016-11-11