Rumah > php教程 > PHP源码 > teks badan

封装 Twitter 访问的 PHP 类

PHP中文网
Lepaskan: 2016-06-01 14:32:35
asal
1122 orang telah melayarinya

跳至

<?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[&#39;http_code&#39;]) == 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(&#39;@(https?://([-\w\.]+)+(d+)?(/([\w/_\.]*(\?\S+)?)?)?)@&#39;, &#39;$1&#39;, $text);
        $text = preg_replace("#(^|[\n ])@([^ \"\t\n\rapi_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(&#39;D jS M y H:i&#39;, 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;
    }

}
Salin selepas log masuk

                   

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan