加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

封装 Twitter 访问的 PHP 类

发布时间:2020-05-25 07:46:27 所属栏目:PHP 来源:互联网
导读:封装 Twitter 访问的 PHP 类

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

<?php

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 ])@([^ "tnr<]*)#ise","'1<a href="http://www.twitter.com/2" >@2</a>'",$text); 
        $text = preg_replace("#(^|[n ])#([^ "tnr<]*)#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>nn";

        $detail_o = "<div class="details">n";
        $detail_c = "</div>nn";

        $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;
    }

}

以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读