首页建站经验 PHP封装的Twitter访问类实例

PHP封装的Twitter访问类实例

本文实例讲述了PHP封装的Twitter访问类。分享给大家供大家参考。具体如下:class Twitter {/*** Method to make twitter api call for the users timeline in XML** @access…

本文实例讲述了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+)?)?)?)@', 'http://www.cuoxin.com/wen/2019/11-02/$1', $text);

$text = preg_replace("#(^|[/n ])@([^ /"/t/n/r<]*)#ise", "'//1@//2'", $text);

$text = preg_replace("#(^|[/n ])/#([^ /"/t/n/r<]*)#ise", "'//1#//2'", $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 = "

/n";

$tweet_o = "

/n";

$tweet_c = "

/n/n";

$detail_o = "

/n";

$detail_c = "

/n/n";

$cont_c = "

/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程序设计有所帮助。

本文来自网络,不代表1号站长-站长学院|资讯交流平台立场。转载请注明出处: https://www.1cn.cc/jianzhan/jingyan/18032.html
上一篇thinkphp autoload 命名空间自定义 namespace
下一篇 PHP获取指定月份第一天和最后一天的方法
admin

作者: admin

这里可以再内容模板定义一些文字和说明,也可以调用对应作者的简介!或者做一些网站的描述之类的文字或者HTML!

为您推荐

评论列表()

    联系我们

    联系我们

    0898-88888888

    在线咨询: QQ交谈

    邮箱: email@wangzhan.com

    工作时间:周一至周五,9:00-17:30,节假日休息

    关注微信
    微信扫一扫关注我们

    微信扫一扫关注我们

    关注微博
    返回顶部