<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tom Reitz &#187; microblogging</title>
	<atom:link href="http://www.tom-reitz.com/tag/microblogging/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tom-reitz.com</link>
	<description>Tech Talk, Catholic Commentary, and American Activism</description>
	<lastBuildDate>Tue, 07 Dec 2010 09:42:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Twitter from PHP</title>
		<link>http://www.tom-reitz.com/2009/06/16/twitter-from-php/</link>
		<comments>http://www.tom-reitz.com/2009/06/16/twitter-from-php/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 17:52:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[microblogging]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://www.tom-reitz.com/?p=143</guid>
		<description><![CDATA[A few months ago I broke down and joined Twitter. Pretty much the only reason is that I wanted to learn how to integrate Twitter into PHP web applications. Here&#8217;s what I found:
Twitter has a URL http://www.twitter.com/statuses/update.xml where you can POST a twitter update. The following HTML form will update your twitter status after asking [...]]]></description>
			<content:encoded><![CDATA[<p>A few months ago I broke down and joined <a title="Twitter: Tom Reitz" href="http://twitter.com/t0mreitz" target="_blank">Twitter</a>. Pretty much the only reason is that I wanted to learn how to integrate Twitter into PHP web applications. Here&#8217;s what I found:</p>
<p>Twitter has a URL <a title="Twitter status update" href="http://www.twitter.com/statuses/update.xml" target="_blank">http://www.twitter.com/statuses/update.xml</a> where you can POST a twitter update. The following HTML form will update your twitter status after asking you for your username and password:</p>
<pre style="padding-left: 30px;">&lt;form action="http://twitter.com/statuses/update.xml" method="POST"&gt;
	&lt;input type="text" name="status" /&gt;
	&lt;input type="submit" value=""Tweet!" /&gt;
&lt;/form&gt;</pre>
<p>This is cool, but it would be nice to circumvent the password protection in a web application where you probably already have a user logged in and validated? Fortunately this is possible using HTTP headers: (please make sure you read my SECURITY NOTE below)</p>
<pre style="padding-left: 30px;">&lt;form action='twitter.php' method='post'&gt;
	&lt;input type='text' name='status' /&gt;
	&lt;input type='submit' value='tweet' /&gt;
&lt;/form&gt;
&lt;?php
	if($_POST['status']!="") {

		$data = "status=".stripslashes($_POST['status']);
		$fp = fsockopen("www.twitter.com", 80);
		$user = "twitter_username";
		$pass = "twitter_password";

		fputs($fp, "POST /statuses/update.xml HTTP/1.1\r\n");
		fputs($fp, "Host: www.twitter.com\r\n");
		fputs($fp, "Referer: None\r\n");
		fputs($fp, "Authorization: Basic ".
			base64_encode($user.":".$pass)."\r\n");
		fputs($fp, "Content-type: ".
			"application/x-www-form-urlencoded\r\n");
		fputs($fp, "Content-length: ". strlen($data) ."\r\n");
		fputs($fp, "Connection: close\r\n\r\n");
		fputs($fp, $data);

		while(!feof($fp))$str .= fgets($fp, 128);
		if(!strstr(" OK ",$str))
			echo "There was a problem posting your twitter update.";
		else echo "twitter update posted successfully!";
	}
?&gt;</pre>
<h3>SECURITY NOTE:</h3>
<p>Please notice that the code above sends the username and password hash <em>unencrypted</em> over a plaintext HTTP pipe. The password hash is thus vulnerable to rainbow table attacks.</p>
<p>A better solution uses HTTPS with <a title="cURL in PHP" href="http://us.php.net/curl" target="_blank">cURL</a>. This way, the username &amp; password hash info don&#8217;t have to be sent plaintext:</p>
<pre style="padding-left: 30px;">&lt;form action='twitter.php' method='post'&gt;
	&lt;input type='text' name='status' /&gt; &lt;input type='submit' value='tweet' /&gt;
&lt;/form&gt;
&lt;?php
	if($_POST['status']!="") {
		$url = "https://twitter.com/statuses/update.xml";
		$user = "twitter_username";
		$pass = "twitter_password";

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_HEADER, true);
		curl_setopt($ch, CURLOPT_USERPWD, $user.":".$pass);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_POSTFIELDS,
			"status=".stripslashes($_POST['status']));

		ob_start();
		curl_exec($ch);
		$str = ob_get_contents();
		ob_end_clean();
		curl_close($ch);

		if($str=="" || strstr($str,"&lt;error&gt;"))
			echo "There was a problem posting your twitter update.";
		else echo "twitter update posted successfully!";
	}
?&gt;</pre>
<p>Since I&#8217;ve started using twitter I&#8217;ve found that it&#8217;s very useful for companies and organizations that want to get messages out to lots of people quickly. You can even get twitter updates by phone using SMS!</p>
<p>Happy tweeting!</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.tom-reitz.com%2F2009%2F06%2F16%2Ftwitter-from-php%2F&amp;linkname=Twitter%20from%20PHP"><img src="/wp-content/themes/organic-theme/img/share_save.gif" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.tom-reitz.com/2009/06/16/twitter-from-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

