PHP Twitter PHP脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP Twitter PHP脚本相关的知识,希望对你有一定的参考价值。

<?php
// the number of tweets we want to display
$tweetsToDisplay = 10;
$username = "username"; // make sure you change this to your username

$twitterrequest = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $username . '&count=' . $tweetsToDisplay;
$twitterci = curl_init($twitterrequest);
curl_setopt($twitterci,CURLOPT_RETURNTRANSFER, TRUE);
$twitterinput = curl_exec($twitterci);
curl_close($twitterci);

// parameter 'true' is necessary for output as PHP array
$tweets = json_decode($twitterinput,true);

echo "<h3><a href=\"http://twitter.com/$username\">Twitter Updates</a></h3>";

if(empty($tweets)){
	echo "Twitter doesn't seem to be working at the moment.";
}else if(!empty($tweets['error'])){
	echo "There was an issue with Twitter.";
}else{
	// Twitter uses the GMT timezone
	date_default_timezone_set('GMT');
	// This is Twitter's response date format
	$curDate = date('D M d H:i:s O Y');
	// then we explode it to use it later
	list($curDay, $curMonth, $curDayNum, $curTime, $curTimezone, $curYear) = explode(" ",$curDate);
	$curTimeArray = explode(":", $curTime);
	// turn the month name into a number
	$curMonthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		for($cm=1;$cm<=12;$cm++){
			if($curMonthNum[$cm] == $curMonth){
				$curMonth = $cm;
			}
		}
	echo "<ul>";
	// a quick bug fix, without this extra bullets may be in the list
	if($tweets[0]['user']['statuses_count'] < $tweetsToDisplay){
		$display = $tweets[0]['user']['statuses_count'] - 1;
	}else{
		$display = $tweetsToDisplay - 1;
	}

	for($i=0;$i<=$display;$i++){
		echo "<li>";
		// we are going to check for any links, @users, or #hashtags
		$tweetText = $tweets[$i]['text'] . "<br />";
		if(preg_match('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',$tweetText,$urlTweet)){
			// if there is, replace that match with an actual link
			$tweetText = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@','<a href="$1">$1</a>',$tweetText);
		}
		// check to see if it's a reply
		if(preg_match('/@([\w]+)/', $tweetText)){
			$tweetText = preg_replace('/@([\w]+)/','<a href="http://twitter.com/$1">@$1</a>',$tweetText);
		}
		// then check for a hash tag
		if(preg_match('/\B#(\w*[a-zA-Z]+\w*)/', $tweetText)){
			$tweetText = preg_replace('/\B#(\w*[a-zA-Z]+\w*)/', '<a href="http://twitter.com/search?q=%23$1">#$1</a>',$tweetText);
		}
		// Finally, we can echo the Tweet
		echo $tweetText;
		// then we figure out the time ago it was sent
		$tweetTime = $tweets[$i]['created_at'];
		list($day, $month, $dayNum, $time, $timezone, $year) = explode(" ", $tweetTime);
		$timeArray = explode(":",$time);
		// Because the month comes in as a string, we need to covert it to a integer
		$monthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		for($m=1;$m<=12;$m++){
			if($monthNum[$m] == $month){
				$month = $m;
			}
		}
		// and then echo it
		// if the month, day, and hour are the same, echo the minutes ago
		if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum && $timeArray[0] == $curTimeArray[0]){
			$timeAgo = $curTimeArray[1] - $timeArray[1];
			if($timeAgo == 0){
				echo "Sent less then a minute ago";
			}else if($timeAgo == 1){
				echo "Sent one minute ago";
			}else if($timeAgo > 50){
				echo "Sent about an hour ago";
			}else{
				echo "Sent " . $timeAgo . " minutes ago";
			}
		// if it was less then a day ago, echo the hours ago
		}else if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum){
			$timeAgo = $curTimeArray[0] - $timeArray[0];
			if($timeAgo > 22){
				echo "Sent about a day ago";
			}else if($timeAgo ==1){
				echo "Sent an hour ago";
			}else{
				echo "Sent " . $timeAgo . " hours ago";
			}
		}else if($year == $curYear && $month == $curMonth){
			$timeAgo = $curDayNum - $dayNum;
			if($timeAgo > 29){
				echo "Sent about a month ago";
			}else if($timeAgo == 1){
				echo "Sent a day ago";
			}else{
				echo "Sent " . $timeAgo . " days ago";
			}
		}else if($year == $curYear){
			$timeAgo = $curMonth - $month;
			if($timeAgo == 1){
				echo "Sent one month ago";
			}else{
				echo "Sent " . $timeAgo . " months ago";
			}
		}else{
			$timeAgo = $curYear - $year;
			if($timeAgo == 1){
				echo "Sent " . $timeAgo . " year ago";
			}else{
				echo "Sent " . $timeAgo . " years ago";
			}
		}
		// if it's a reply, append a "In reply to at the end"
		if($tweets[$i]['in_reply_to_screen_name'] !== NULL){
			echo " in reply to <a href=\"http://twitter.com/" . $tweets[$i]['in_reply_to_screen_name'] . "\">" . $tweets[$i]['in_reply_to_screen_name'] . "</a>.";
		}else{
			echo ".";
		}
		echo "</li>";
	}
	echo "</ul>";
	echo "<p><a href=\"http://twitter.com/$username\">Follow Me</a></p>";
}
?>

以上是关于PHP Twitter PHP脚本的主要内容,如果未能解决你的问题,请参考以下文章

Twitter PHP脚本

Twitter自动跟踪PHP脚本

PHP Twitter跟随脚本修改

PHP - Twitter API - 使用空间时请求损坏

Twitter Bootstrap HTML 表单单选按钮不适用于 PHP POST

8个PHP显示加密货币市场实时价格的脚本