如何在我的PHP网站上显示Twitter关注者?
我已附上我的演示图像。你能建议我如何在我的网站上实现这个用例吗如何在我的PHP网站上显示Twitter关注者?,php,twitter,Php,Twitter,我已附上我的演示图像。你能建议我如何在我的网站上实现这个用例吗 :请求很大,但它在这里: 我已经修改了我以前做过的一些代码,这样你就可以在不通过twitter获得誓言密钥/秘密的情况下完成这项工作。它不是完美的,但是这段代码应该按原样运行,而不需要做任何额外的事情 将所有这些文件保存在与我指定的名称相同的文件夹中: 在第2行编辑$twitter\u handle的值,在第28行编辑$bit\u cheetah的变量名,以反映您的@twitter\u handle而不带“@” 然后运行twit.
:请求很大,但它在这里:
- 我已经修改了我以前做过的一些代码,这样你就可以在不通过twitter获得誓言密钥/秘密的情况下完成这项工作。它不是完美的,但是这段代码应该按原样运行,而不需要做任何额外的事情
- 将所有这些文件保存在与我指定的名称相同的文件夹中:
- 在第2行编辑$twitter\u handle的值,在第28行编辑$bit\u cheetah的变量名,以反映您的@twitter\u handle而不带“@”
- 然后运行twit.php,你就应该拥有你想要的东西了
<?php //Twitter_Class.php
class Twitter {
public $handle;
public $avatar;
public $name;
public $usrURL;
public $count;
public $xml;
public $DOM;
public $status = array();
public $statusTime = array();
public function __construct( $twitter_id, $count ) {
$this->handle = $twitter_id;
$this->count = $count;
$this->cTwitter();
$this->cDOM();
$this->allUserInfo();
$this->allStatus();
}
private function cTwitter() {
$cURL = curl_init();
curl_setopt( $cURL, CURLOPT_URL, "http://api.twitter.com/1/statuses/user_timeline.xml?include_entities=true&include_rts=true&screen_name=".$this->handle."&count=".$this->count );
curl_setopt( $cURL, CURLOPT_RETURNTRANSFER, 1 );
$this->xml = curl_exec( $cURL );
curl_close( $cURL );
}
private function cDOM() {
$DD = new DOMDocument;
$DD->loadXML( $this->xml );
$this->DOM = simplexml_import_dom( $DD );
}
private function allUserInfo() {
$this->name = $this->DOM->status[ 0 ]->user->name;
$this->avatar = $this->DOM->status[ 0 ]->user->profile_image_url;
$this->usrURL = 'https://twitter.com/'.$this->handle;
}
private function allStatus() {
for( $i=0; $i<$this->count; $i++ ) {
$this->status[ $i ] = $this->DOM->status[ $i ]->text;
$this->statusTime[ $i ] = strtotime( $this->DOM->status[ $i ]->created_at );
$urlSwapper = explode( ' ', $this->status[ $i ] );
$URL = '';
$at_handle = '';
foreach( $urlSwapper as $uS ) {
if( preg_match( '/^http:\/\/.*$/i', $uS, $matches ) ) {
$URL = $matches[0];
}
}
$this->status[ $i ] = ereg_replace( "[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", '<a href="'.$URL.'" target="_blank" class="tweet_link">\\0</a>', $this->status[ $i ] );
//$this->status[ $i ] = str_replace( '/^@.*$/i', );
}
}
public function getAvatar() {
if( isset( $this->avatar ) )
return $this->avatar;
else
return 'Avatar not set!';
}
public function getName() {
if( isset( $this->name ) )
return $this->name;
else
return 'Name not set!';
}
public function getStatus( $num ) {
if( isset( $this->status[ $num ] ) )
return $this->status[ $num ];
else
return 'getStatus( '.$num.' ) is not set!';
}
public function getStatusTime( $num ) {
if( isset( $this->statusTime[ $num ] ) )
return $this->statusTime[ $num ];
else
return 'getStatusTime( '.$num.' ) is not set!';
}
public function __destruct() {
if( isset( $this->handle ) )
unset( $this->handle );
if( isset( $this->avatar ) )
unset( $this->avatar );
if( isset( $this->name ) )
unset( $this->name );
if( isset( $this->count ) )
unset( $this->count );
if( isset( $this->xml ) )
unset( $this->xml );
if( isset( $this->DOM ) )
unset( $this->DOM );
if( isset( $this->status ) )
unset( $this->status );
if( isset( $this->usrURL ) )
unset( $this->usrURL );
if( isset( $this->statusTime ) )
unset( $this->statusTime );
}
}
?>
<?php
$twitter_handle = 'bit_cheetah'; # <-- Change to your @handle without the '@';
$tweet_count = 4;
?>
<!DOCTYPE html>
<html>
<head>
<title> Twitter Stream ;p </title>
<link rel="stylesheet" type="text/css" href="twit.css" />
</head>
<body>
<br />
<br />
<br />
<?php
echo '<center><span style="text-align:left;"><h2>Latest Tweets</h2>';
$status_times = array();
$status = array();
$status_who = array();
$sT_ctr = 0;
$s_ctr = 0;
include( 'Twitter_Class.php' );
$bit_cheetah = new Twitter( $twitter_handle, $tweet_count ); # <-- LINE 28 Change $handle to your $twitter_handle!!
function TWIT_LOAD( $who, &$status_times, &$status, &$status_who, &$sT_ctr, &$s_ctr ) {
if( count( $GLOBALS[ $who ]->statusTime ) == 1 || count( $GLOBALS[ $who ]->statusTime ) == 0 ) {
$status_times[ 0 ] = $GLOBALS[ $who ]->statusTime[0];
}
else {
foreach( $GLOBALS[ $who ]->statusTime as $sT ) {
$status_times[ $sT_ctr++ ] = $sT;
}
}
foreach( $GLOBALS[ $who ]->status as $s ) {
$status[ $s_ctr ] = $s;
$status_who[ $s_ctr++ ] = $who;
}
return $who;
}
function TWEET( $who, $what, $when ) {
$avatar = $GLOBALS[ $who ]->avatar;
$name = $GLOBALS[ $who ]->name;
$usrURL = $GLOBALS[ $who ]->usrURL;
$handle = '@'.$GLOBALS[ $who ]->handle;
$now = time();
$time_diff = $now - $when;
$min = ( int )( $time_diff / 60 );
if( $min < 60 ) {
$time = $min.'m';
}
elseif( $min > 60 AND $min < 1440 ) {
$hour = ( int )( $min / 60 );
$time = $hour.'h';
}
elseif( $min > 1440 ) {
$day = ( int )( $min / 1440 );
$time = $day.'d';
}
else {
$time = '?m';
}
$status = $what;
$RET .= '<tr valign="top">';
$RET .= '<td><img src="'.$avatar.'" class="avatar" target="_blank" /></td>';
$RET .= '<td><table class="tweet_core">';
$RET .= '<tr valign="bottom">';
$RET .= '<td><table class="tweet_top">';
$RET .= '<tr valign="bottom">';
$RET .= '<td align="left"><span class="name"><a href="'.$usrURL.'" target="_blank" class="name" onmouseover="this.style.color=\'#777\';" onmouseout="this.style.color=\'#333\';" title="View '.$name.'\'s Profile">'.$name.'</a></span><span class="handle">'.$handle.'</span></td>';
$RET .= '<td align="right"><span class="time">'.$time.'</span></td>';
$RET .= '</tr></table></td></tr>';
$RET .= '<tr valign="bottom">';
$RET .= '<td><span class="status">'.$status.'</span></td>';
$RET .= '</tr></table>';
$RET .= '</td></tr>';
return $RET;
}
$PEEPS = array( $twitter_handle );
foreach( $PEEPS as $P ) {
$RET = TWIT_LOAD( $P, $status_times, $status, $status_who, $sT_ctr, $s_ctr );
}
arsort( $status_times );
echo '<table class="tweet_whole" cellspacing="0" border="0"><tr><td>';
echo '<table class="tweet" cellspacing="0">';
foreach( $status_times as $key => $val ) {
$tweet = TWEET( $status_who[ $key ], $status[ $key ], $status_times[ $key ] );
echo $tweet;
}
echo '</table>';
echo '</td></tr></table>';
echo '<div style="text-align:left;"><a href="https://twitter.com/intent/user?screen_name='.$twitter_handle.'" target="_blank" style="text-decoration:none;"><button style="padding-left:3px;padding-right:3px;color:darkblue;font-weight:600">Follow us on Twitter</button></a></div>';
echo '</span></center>';
?>
<br />
<br />
<br />
</body>
</html>
.:
→ 文件#3:twit.php
<?php //Twitter_Class.php
class Twitter {
public $handle;
public $avatar;
public $name;
public $usrURL;
public $count;
public $xml;
public $DOM;
public $status = array();
public $statusTime = array();
public function __construct( $twitter_id, $count ) {
$this->handle = $twitter_id;
$this->count = $count;
$this->cTwitter();
$this->cDOM();
$this->allUserInfo();
$this->allStatus();
}
private function cTwitter() {
$cURL = curl_init();
curl_setopt( $cURL, CURLOPT_URL, "http://api.twitter.com/1/statuses/user_timeline.xml?include_entities=true&include_rts=true&screen_name=".$this->handle."&count=".$this->count );
curl_setopt( $cURL, CURLOPT_RETURNTRANSFER, 1 );
$this->xml = curl_exec( $cURL );
curl_close( $cURL );
}
private function cDOM() {
$DD = new DOMDocument;
$DD->loadXML( $this->xml );
$this->DOM = simplexml_import_dom( $DD );
}
private function allUserInfo() {
$this->name = $this->DOM->status[ 0 ]->user->name;
$this->avatar = $this->DOM->status[ 0 ]->user->profile_image_url;
$this->usrURL = 'https://twitter.com/'.$this->handle;
}
private function allStatus() {
for( $i=0; $i<$this->count; $i++ ) {
$this->status[ $i ] = $this->DOM->status[ $i ]->text;
$this->statusTime[ $i ] = strtotime( $this->DOM->status[ $i ]->created_at );
$urlSwapper = explode( ' ', $this->status[ $i ] );
$URL = '';
$at_handle = '';
foreach( $urlSwapper as $uS ) {
if( preg_match( '/^http:\/\/.*$/i', $uS, $matches ) ) {
$URL = $matches[0];
}
}
$this->status[ $i ] = ereg_replace( "[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", '<a href="'.$URL.'" target="_blank" class="tweet_link">\\0</a>', $this->status[ $i ] );
//$this->status[ $i ] = str_replace( '/^@.*$/i', );
}
}
public function getAvatar() {
if( isset( $this->avatar ) )
return $this->avatar;
else
return 'Avatar not set!';
}
public function getName() {
if( isset( $this->name ) )
return $this->name;
else
return 'Name not set!';
}
public function getStatus( $num ) {
if( isset( $this->status[ $num ] ) )
return $this->status[ $num ];
else
return 'getStatus( '.$num.' ) is not set!';
}
public function getStatusTime( $num ) {
if( isset( $this->statusTime[ $num ] ) )
return $this->statusTime[ $num ];
else
return 'getStatusTime( '.$num.' ) is not set!';
}
public function __destruct() {
if( isset( $this->handle ) )
unset( $this->handle );
if( isset( $this->avatar ) )
unset( $this->avatar );
if( isset( $this->name ) )
unset( $this->name );
if( isset( $this->count ) )
unset( $this->count );
if( isset( $this->xml ) )
unset( $this->xml );
if( isset( $this->DOM ) )
unset( $this->DOM );
if( isset( $this->status ) )
unset( $this->status );
if( isset( $this->usrURL ) )
unset( $this->usrURL );
if( isset( $this->statusTime ) )
unset( $this->statusTime );
}
}
?>
<?php
$twitter_handle = 'bit_cheetah'; # <-- Change to your @handle without the '@';
$tweet_count = 4;
?>
<!DOCTYPE html>
<html>
<head>
<title> Twitter Stream ;p </title>
<link rel="stylesheet" type="text/css" href="twit.css" />
</head>
<body>
<br />
<br />
<br />
<?php
echo '<center><span style="text-align:left;"><h2>Latest Tweets</h2>';
$status_times = array();
$status = array();
$status_who = array();
$sT_ctr = 0;
$s_ctr = 0;
include( 'Twitter_Class.php' );
$bit_cheetah = new Twitter( $twitter_handle, $tweet_count ); # <-- LINE 28 Change $handle to your $twitter_handle!!
function TWIT_LOAD( $who, &$status_times, &$status, &$status_who, &$sT_ctr, &$s_ctr ) {
if( count( $GLOBALS[ $who ]->statusTime ) == 1 || count( $GLOBALS[ $who ]->statusTime ) == 0 ) {
$status_times[ 0 ] = $GLOBALS[ $who ]->statusTime[0];
}
else {
foreach( $GLOBALS[ $who ]->statusTime as $sT ) {
$status_times[ $sT_ctr++ ] = $sT;
}
}
foreach( $GLOBALS[ $who ]->status as $s ) {
$status[ $s_ctr ] = $s;
$status_who[ $s_ctr++ ] = $who;
}
return $who;
}
function TWEET( $who, $what, $when ) {
$avatar = $GLOBALS[ $who ]->avatar;
$name = $GLOBALS[ $who ]->name;
$usrURL = $GLOBALS[ $who ]->usrURL;
$handle = '@'.$GLOBALS[ $who ]->handle;
$now = time();
$time_diff = $now - $when;
$min = ( int )( $time_diff / 60 );
if( $min < 60 ) {
$time = $min.'m';
}
elseif( $min > 60 AND $min < 1440 ) {
$hour = ( int )( $min / 60 );
$time = $hour.'h';
}
elseif( $min > 1440 ) {
$day = ( int )( $min / 1440 );
$time = $day.'d';
}
else {
$time = '?m';
}
$status = $what;
$RET .= '<tr valign="top">';
$RET .= '<td><img src="'.$avatar.'" class="avatar" target="_blank" /></td>';
$RET .= '<td><table class="tweet_core">';
$RET .= '<tr valign="bottom">';
$RET .= '<td><table class="tweet_top">';
$RET .= '<tr valign="bottom">';
$RET .= '<td align="left"><span class="name"><a href="'.$usrURL.'" target="_blank" class="name" onmouseover="this.style.color=\'#777\';" onmouseout="this.style.color=\'#333\';" title="View '.$name.'\'s Profile">'.$name.'</a></span><span class="handle">'.$handle.'</span></td>';
$RET .= '<td align="right"><span class="time">'.$time.'</span></td>';
$RET .= '</tr></table></td></tr>';
$RET .= '<tr valign="bottom">';
$RET .= '<td><span class="status">'.$status.'</span></td>';
$RET .= '</tr></table>';
$RET .= '</td></tr>';
return $RET;
}
$PEEPS = array( $twitter_handle );
foreach( $PEEPS as $P ) {
$RET = TWIT_LOAD( $P, $status_times, $status, $status_who, $sT_ctr, $s_ctr );
}
arsort( $status_times );
echo '<table class="tweet_whole" cellspacing="0" border="0"><tr><td>';
echo '<table class="tweet" cellspacing="0">';
foreach( $status_times as $key => $val ) {
$tweet = TWEET( $status_who[ $key ], $status[ $key ], $status_times[ $key ] );
echo $tweet;
}
echo '</table>';
echo '</td></tr></table>';
echo '<div style="text-align:left;"><a href="https://twitter.com/intent/user?screen_name='.$twitter_handle.'" target="_blank" style="text-decoration:none;"><button style="padding-left:3px;padding-right:3px;color:darkblue;font-weight:600">Follow us on Twitter</button></a></div>';
echo '</span></center>';
?>
<br />
<br />
<br />
</body>
</html>
推特流;P
到目前为止,您尝试了什么?您想如何实现它。。通过API或?显示您的努力。你完成了吗?请参考并开始写一些代码。你不必为他完成工作,但需要付出努力。哈,已经完成了,只需要做一些改动。如果我做了这件事;我会好10倍。。可能也会使用誓言@Hiroto:p@BITCHEETAH:此错误显示(致命错误:调用C:\wamp\www\test\twitter\twitter\u Class.php第23行中未定义的函数curl\u init())@sumitdadhich-See:或@bitcheah:请定义如何更改代码以进行工作