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