PHP封装的Twitter访问类实例
本文实例讲述了PHP封装的Twitter访问类。分享给大家供大家参考。具体如下:
classTwitter{
/**
*MethodtomaketwitterapicallfortheuserstimelineinXML
*
*@accessprivate
*@param$twitter_id,$num_of_tweets
*@return$xml
*/
privatefunctionapi_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=newSimpleXMLElement($response);
return$xml;
}else{
returnfalse;
}
}
/**
*Methodtoaddhyperlinkhtmltagstoanyurls,twitteridsorhashtagsintweet
*
*@accessprivate
*@param$text
*@return$text
*/
privatefunctionprocess_links($text){
$text=utf8_decode($text);
$text=preg_replace('@(https?://([-\w\.]+)+(d+)?(/([\w/_\.]*(\?\S+)?)?)?)@','<ahref="$1">$1</a>',$text);
$text=preg_replace("#(^|[\n])@([^\"\t\n\r<]*)#ise","'\\1<ahref=\"http://www.twitter.com/\\2\">@\\2</a>'",$text);
$text=preg_replace("#(^|[\n])\#([^\"\t\n\r<]*)#ise","'\\1<ahref=\"http://hashtags.org/search?query=\\2\">#\\2</a>'",$text);
return$text;
}
/**
*Mainmethodtoretrievethetweetsandreturnhtmlfordisplay
*
*@accesspublic
*@param$twitter_id,$num_of_tweets,$timezone
*@return$result
*/
publicfunctionget_tweets($twitter_id,$num_of_tweets=3,$timezone="America/Denver"){
$include_replies=false;
date_default_timezone_set($timezone);
//thehtmlmarkup
$cont_o="<divid=\"tweets\">\n";
$tweet_o="<divclass=\"status\">\n";
$tweet_c="</div>\n\n";
$detail_o="<divclass=\"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->statusas$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('DjSMyH:i',strtotime($status->created_at)).$detail_c;
}
}
$result.=$cont_c;
}else{
$result.=$cont_o.$tweet_o."Twitterseemstobeunavailableatthemoment.".$tweet_c.$cont_c;
}
return$result;
}
}
希望本文所述对大家的php程序设计有所帮助。