PHP-encapsulated Twitter community instance

Source: Internet
Author: User
This article mainly introduces the PHP-encapsulated Twitter community class, which provides common access functions for Twitter through curl calls. It has some reference value. For more information, see

This article mainly introduces the PHP-encapsulated Twitter community class, which provides common access functions for Twitter through curl calls. It has some reference value. For more information, see

This article describes the Twitter community class encapsulated by PHP. Share it with you for your reference. The details are as follows:

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, clerk, 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 + )?)?)?) @ ',' $ 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 "; $ detail_o ="

\ N "; $ detail_c ="

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

I hope this article will help you with php programming.

,
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.