PHP Twitter跟随脚本修改

Posted

tags:

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

<html>
<head>
<title>Twitter Follow Script - Jonathon Maguire</title>
</head>
<body>
<center>
<u><h1><i>Twitter Follow Script</i></h1></u>
<style> 
div { 
border: .2em dotted #900; 
} 
#border { 
border-width: .2em; 
border-style: solid; 
border-color: red; 
} 
</style>
<form method="post" action="">
<div id="border">
<p><u>Username:</u></p>
<input type="text" name="username" value="">
<br><p><u>Password:</u></p>
<input type="password" name="password" value="">
<br><p><u>Search Term</u></p>
<input type="text" name="term" value=""><br>
<input type="submit" name="submit" value="submit">
</div>
</center>

</body>
</html>
<?php

// Twitter Auto-follow Script by Dave Stevens - http://davestevens.co.uk, Modified by Jonathon Maguire, http://www.unknowntruth.net

if(isset($_POST['submit']))
{
// Set the twitter user
$user = $_POST['username'];
$pass = $_POST['password'];
 
// Set the term you want to follow (e.g. "soccer")
$term = $_POST['term'];
 
// Get already followed
$userApiUrl = "http://twitter.com/statuses/friends.json";

if(isset($_POST['submit'])) 
$ch = curl_init($userApiUrl);
curl_setopt($ch, CURLOPT_USERPWD, $user.":".$pass);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
$apiresponse = curl_exec($ch);
 
curl_close($ch);
 
$followed = array();
 
if ($apiresponse) {
	$json = json_decode($apiresponse);
	if ($json != null) {
		foreach ($json as $u) {
			$followed[] = $u->name;
		}
	}
}
 
$userApiUrl = "http://search.twitter.com/search.json?q=" . $term . "&rpp=100";
 
$ch = curl_init($userApiUrl);
curl_setopt($ch, CURLOPT_USERPWD, $user.":".$pass);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
$apiresponse = curl_exec($ch);
 
curl_close($ch);
 
if ($apiresponse) {
	$results = json_decode($apiresponse);
	$count = 90;
 
	if ($results != null) {
 
		$resultsArr = $results->results;
 
		if (is_array($resultsArr)) {
 
			foreach ($resultsArr as $result) {
 
				$from_user = $result->from_user;
 
				if (!in_array($from_user,$followed)) {
 
					$ch = curl_init("http://twitter.com/friendships/create/" . $from_user . ".json");
					curl_setopt($ch, CURLOPT_USERPWD, $user.":".$pass);
					curl_setopt($ch, CURLOPT_POST, 1);
					curl_setopt($ch, CURLOPT_POSTFIELDS,"follow=true");					
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
					$apiresponse = curl_exec($ch);
 
					if ($apiresponse) {
						$response = json_decode($apiresponse);
 
						if ($response != null) {
							if (property_exists($response,"following")) {
								if ($response->following === true) {
									echo "Now following " . $response->screen_name . "\n";
								} else {
									echo "Couldn't follow " . $response->screen_name . "\n";
								}
							} else {
								echo "Follow limit exceeded, skipped " . $from_user . "\n";
							}
						}
 
					}
 
					curl_close($ch);
 
				} else {
					echo "Already following " . $from_user . "\n";
				}
 
			}
 
		}	
 
	}
 
 
}
}
 
?>

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

PHP Twitter自动关注PHP脚本

PHP Twitter PHP脚本

Twitter PHP脚本

Twitter自动跟踪PHP脚本

Twitter Follow脚本已修改

ruby 感谢跟随我使用ruby的twitter bot