我附上了我的演示图片。你能否建议我如何在我的网站上实现这个用例?
答案 0 :(得分:1)
:大请求,但这里是:。
我已经调整了一些我以前做过的代码,所以你可以在没有获得OATH密钥/秘密的情况下完成此操作。这是不完美但是这段代码应该按原样运行而不需要做任何额外的事情。
将所有这些文件保存在与我指定名称相同的文件夹中:
在第2行修改 $ twitter_handle 的值,在第28行修改 $ bit_cheetah的变量名称以反映 @twitter_handle 没有'@'
然后运行 twit.php ,你应该拥有你想要的东西。
:
:
→文件#1: Twitter_Class.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 );
}
}
?>
:
→文件#2: twit.css
/* file: twit.css */
* {
margin: 0px;
}
.name {
vertical-align: bottom;
font-family: Arial;
}
.handle {
vertical-align: top;
font-family: Arial;
font-style: italic;
font-size: 10pt;
color: #333;
}
.usrTweet {
font-family: Arial;
font-size: 12pt;
width: 25%;
line-height: 1.2em;
}
.usrURL {
text-decoration: none;
color: #000;
}
.tweet_time {
font-family: Arial;
font-size: 8pt;
color: #777;
}
.time_table {
width: 100%;
text-align: right;
padding-top: 7px;
}
table.tweet_whole {
width: 36%;
border-right: solid 1px #e8e8e8;
border-left: solid 1px #e8e8e8;
border-top: solid 1px #e8e8e8;
border-bottom: solid 1px #e8e8e8;
}
table.tweet {
width: 100%;
border-collapse: collapse;
border-style: hidden;
}
table.tweet tr {
border-bottom: solid 1px #e8e8e8;
border-top: 0px;
border-left: 0px;
border-right: 0px;
}
table.tweet:first-child tr {
border-bottom: solid 1px #e8e8e8;
border-top: solid 1px #e8e8e8;
border-left: 0px;
border-right: 0px;
}
table.tweet_core {
width: 100%;
padding-bottom: 7px;
}
table.tweet_top {
width: 100%;
}
span.name {
font-family: Arial;
font-weight: 600;
font-size: 13pt;
color: #333;
padding-left: 0px;
}
a.name {
text-decoration: none;
color: #333;
}
a.tweet_link {
color: #0084b4;
}
span.handle {
font-family: Arial;
font-weight: 400;
font-size: 10pt;
color: #a5a5a4;
padding-left: 5px;
position: relative;
top: 3px;
}
span.time {
font-family: Arial;
font-weight: 400;
font-size: 10pt;
color: #a3a3a2;
padding-right: 12px;
text-align: right;
position: relative;
top: -1px;
left: 15px;
}
span.status {
font-family: Arial;
font-weight: 400;
font-size: 12pt;
color: #333;
padding-left: 0px;
padding-bottom: 11px;
padding-right: 17px;
}
img.avatar {
width: 48px;
height: 48px;
border: 0px;
padding-left: 12px;
padding-top: 12px;
padding-right: 10px;
padding-bottom: 11px;
vertical-align: top;
}
:
→文件#3: twit.php
<?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>