本文实例讲述了PHP封装的Twitter访问类。分享给大家供大家参考。具体如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
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程序设计有所帮助。
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 49
-
2025-05-29 14
-
2025-05-25 30
-
2025-06-04 38
-
2025-06-04 26
热门评论