国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

PHP封裝的Twitter訪問類實(shí)例

2024-05-04 23:37:54
字體:
供稿:網(wǎng)友

這篇文章主要介紹了PHP封裝的Twitter訪問類,通過curl調(diào)用實(shí)現(xiàn)針對Twitter的常用訪問功能,具有一定參考借鑒價值,需要的朋友可以參考下

本文實(shí)例講述了PHP封裝的Twitter訪問類。分享給大家供大家參考。具體如下:

 

 
  1. class Twitter { 
  2. /** 
  3. * Method to make twitter api call for the users timeline in XML 
  4. * 
  5. * @access private 
  6. * @param $twitter_id, $num_of_tweets 
  7. * @return $xml 
  8. */ 
  9. private function api_call($twitter_id, $num_of_tweets) { 
  10. $c = curl_init(); 
  11. curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$twitter_id.xml?count=$num_of_tweets"); 
  12. curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
  13. curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 3); 
  14. curl_setopt($c, CURLOPT_TIMEOUT, 5); 
  15. $response = curl_exec($c); 
  16. $response_info = curl_getinfo($c); 
  17. curl_close($c); 
  18. if (intval($response_info['http_code']) == 200) { 
  19. $xml = new SimpleXMLElement($response); 
  20. return $xml; 
  21. else { 
  22. return false
  23. /** 
  24. * Method to add hyperlink html tags to any urls, twitter ids or hashtags in tweet 
  25. * 
  26. * @access private 
  27. * @param $text 
  28. * @return $text 
  29. */ 
  30. private function process_links($text) { 
  31. $text = utf8_decode($text); 
  32. $text = preg_replace('@(https?://([-/w/.]+)+(d+)?(/([/w/_/.]*(/?/S+)?)?)?)@''<a href="$1">$1</a>', $text); 
  33. $text = preg_replace("#(^|[/n ])@([^ /"/t/n/r<]*)#ise""'//1<a href=/"http://www.twitter.com///2/" >@//2</a>'", $text); 
  34. $text = preg_replace("#(^|[/n ])/#([^ /"/t/n/r<]*)#ise""'//1<a href=/"http://hashtags.org/search?query=//2/" >#//2</a>'", $text); 
  35. return $text; 
  36. /** 
  37. * Main method to retrieve the tweets and return html for display 
  38. * 
  39. * @access public 
  40. * @param $twitter_id, $num_of_tweets, $timezone 
  41. * @return $result 
  42. */ 
  43. public function get_tweets($twitter_id, $num_of_tweets = 3, $timezone = "America/Denver") { 
  44. $include_replies = false
  45. date_default_timezone_set($timezone); 
  46. // the html markup 
  47. $cont_o = "<div id=/"tweets/">/n"
  48. $tweet_o = "<div class=/"status/">/n"
  49. $tweet_c = "</div>/n/n"
  50. $detail_o = "<div class=/"details/">/n"
  51. $detail_c = "</div>/n/n"
  52. $cont_c = "</div>/n"
  53. if ($twitter_xml = $this->api_call($twitter_id, $num_of_tweets)) { 
  54. $result = $cont_o; 
  55. foreach ($twitter_xml->status as $key => $status) { 
  56. if ($include_replies == true | substr_count($status->text, "@") == 0 | strpos($status->text, "@") != 0) { 
  57. $tweet = $this->process_links($status->text); 
  58. $result .= $tweet_o . $tweet . $tweet_c . $detail_o . date('D jS M y H:i', strtotime($status->created_at)) . $detail_c; 
  59. $result .= $cont_c; 
  60. else { 
  61. $result .= $cont_o . $tweet_o . "Twitter seems to be unavailable at the moment." . $tweet_c . $cont_c; 
  62. return $result; 

希望本文所述對大家的php程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 雅江县| 麦盖提县| 望谟县| 天台县| 叶城县| 简阳市| 蓝山县| 晋江市| 隆回县| 肥东县| 托克托县| 阳西县| 泾阳县| 海兴县| 六安市| 巴青县| 侯马市| 湘潭市| 梅州市| 伊金霍洛旗| 海原县| 贡山| 龙江县| 京山县| 四子王旗| 双峰县| 霍林郭勒市| 北川| 崇左市| 克拉玛依市| 延安市| 时尚| 宜君县| 手游| 正镶白旗| 门头沟区| 大荔县| 繁峙县| 柳江县| 富源县| 科技|