Subscribe to Collegiate Chronicles        RSS Feed
-----

Twitter API Applied

Icon Leave Comment
My project for Week 3 of the 52 Weeks of Code Challenge - a simple Twitter status update application.

The interface:
Posted Image

The main code:
<?php
$username ='username';
$password ='password';

require('API.php');
if(isset($_POST['msg'])){
	$status=$_POST['msg'];
	if(strlen($status)<1){
		$err=1; //Nothing entered in form.
	} else if (strlen($status)>140) {
		$err=2; //Too much data entered.
	} else {
		$update=twitPost($username, $password, $status);
	}
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>52 Weeks of Code :: Week 3 :: Twitter API Applied</title>
</head>

<body>
<h2>Update Twitter</h2>

<?php if(isset($err)){
		if ($err==1) {
			echo "<p style='color:red;font-weight:bold;'>j00 din't enters a msg.</p>";
		} else if ($err==2) {
			echo "<p style='color:red;font-weight:bold;'>j00 enters 2 much msg.</p>";
		} else {
			//No message to display.
		}
	}
?>

<p><strong>What's a-happening, hot stuff?</strong></p>
<form action="twil.php" method="post">
<input name="msg" type="text" id="msg" />
<input type="submit" name="button" value="Submit" />
</form>

</body>
</html>


The function code (API.php):
<?php

function twitPost($username,$password,$msg){

	$cnx = "http://twitter.com/statuses/update.xml?status=".urlencode(stripslashes(urldecode($msg)));

	$cURL = curl_init();
	curl_setopt($cURL, CURLOPT_URL, $cnx);
	curl_setopt($cURL, CURLOPT_VERBOSE, 1);
	curl_setopt($cURL, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($cURL, CURLOPT_USERPWD, "$username:$password");
	curl_setopt($cURL, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
	curl_setopt($cURL, CURLOPT_POST, 1);

	$result = curl_exec($cURL);
	$array = curl_getinfo($cURL);

	curl_close($cURL);

	if($array['http_code'] == "200"){
		 $twitter='Ur msg rawks.';
	} else {
		 $twitter="Ur msg iz teh suxx0rz.";
	}
	return $twitter;
}

?>

0 Comments On This Entry

 

November 2014

S M T W T F S
      1
2345678
9101112131415
16171819202122
23242526 27 2829
30      

Recent Entries

Recent Comments

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)