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

首頁 > 網站 > 建站經驗 > 正文

PHP封裝的Twitter訪問類實例

2024-04-25 20:39:32
字體:
來源:轉載
供稿:網友

本文實例講述了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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 买车| 蒙城县| 南木林县| 贵德县| 固镇县| 平和县| 汽车| 信阳市| 涿州市| 房山区| 绥芬河市| 水城县| 城口县| 延安市| 绥德县| 东源县| 湘乡市| 梧州市| 友谊县| 星座| 隆回县| 霍林郭勒市| 洪洞县| 西宁市| 兰西县| 高要市| 囊谦县| 桐柏县| 尼木县| 河北省| 鞍山市| 宜兴市| 泾源县| 古田县| 井冈山市| 衢州市| 安多县| 迁安市| 新昌县| 峡江县| 南丹县|