PHP封装的Twitter访问类实例

yipeiwu_com5年前PHP代码库

本文实例讲述了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程序设计有所帮助。

相关文章

PHP设计模式之工厂模式实例总结

本文实例讲述了PHP设计模式之工厂模式。分享给大家供大家参考,具体如下: 使用工厂模式的目的或目标? 工厂模式的最大优点在于创建对象上面,就是把创建对象的过程封装起来,这样随时可以产生一...

php中通过eval实现字符串格式的计算公式

有时候我们对每一种产品都有一个提成公式,而这个计算提成的公式是以字符串格式存在表中的当我们用这个计算公式时,他并不像我们写的:$a=2+3*5;这样简单的能计算出结果,而它是个字符串.所...

php 传值赋值与引用赋值的区别

传值赋值:当将一个表达式的值赋予一个变量时,整个原始表达式的值被赋予到目标变量。这意味着,例如,当一个变量的值赋予另一个变量时,改变其中一个变量的值,将不会影响到另一个变量。 复制代码...

hessian 在PHP中的使用介绍

hessian 在PHP中的使用介绍

一、hessian是什么? 看到这个单词我还不知道怎么读,音标是[hes]读黑森。 Hessian是一个轻量级的远程的数据交换工具,使用简单的方法提供了RMI(远程方法调用)的功能. 相...

PHP+JS实现批量删除数据功能示例

本文实例讲述了PHP+JS实现批量删除数据功能。分享给大家供大家参考,具体如下: 表单 <form id="form2" name="form2" method="post"...