<?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>Seichleon &#187; php Tutorial</title>
	<atom:link href="http://seich.martianwabbit.com/category/tutorial/php-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://seich.martianwabbit.com</link>
	<description>Legendarido...</description>
	<lastBuildDate>Wed, 08 Sep 2010 00:27:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How PHP works</title>
		<link>http://seich.martianwabbit.com/how-php-works/</link>
		<comments>http://seich.martianwabbit.com/how-php-works/#comments</comments>
		<pubDate>Mon, 22 Feb 2010 03:19:47 +0000</pubDate>
		<dc:creator>Seich</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Screencast]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[php Tutorial]]></category>

		<guid isPermaLink="false">http://seich.martianwabbit.com/?p=198</guid>
		<description><![CDATA[Okay, this won&#8217;t be of much help to everyone but those following my new screencast series about PHP.  I&#8217;ll be going over the basics and I thought that this would be the best place to start on. &#8220;How PHP works&#8221; shows in about a minute the basic functionality of PHP. I&#8217;ll be going over setting [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, this won&#8217;t be of much help to everyone but those following my new screencast series about PHP.  I&#8217;ll be going over the basics and I thought that this would be the best place to start on. &#8220;How PHP works&#8221; shows in about a minute the basic functionality of PHP. I&#8217;ll be going over setting PHP up in your PC afterwards and then I&#8217;ll start showing the basics of PHP. Hopefully this screencast series will give me a reason to be more active on my blog.</p>
<p><embed src="http://blip.tv/play/haEEgcfCfAA%2Em4v" type="application/x-shockwave-flash" width="400" height="294" allowscriptaccess="always" allowfullscreen="true"></embed></p>
]]></content:encoded>
			<wfw:commentRss>http://seich.martianwabbit.com/how-php-works/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building a Simple Twitter PHP Class</title>
		<link>http://seich.martianwabbit.com/building-a-simple-twitter-php-class/</link>
		<comments>http://seich.martianwabbit.com/building-a-simple-twitter-php-class/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 22:01:55 +0000</pubDate>
		<dc:creator>Seich</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[php Tutorial]]></category>

		<guid isPermaLink="false">http://seich.martianwabbit.com/?p=149</guid>
		<description><![CDATA[PHP is a very versatile scripting language. In this tutorial I will be explaining how to create a PHP class that will help you post and get information from a twitter account using the client URL library (cURL). Tutorial Details Program: PHP/Twitter API Version: 1 Difficulty: Easy Estimated Completion Time: 10 minutes Step 1 &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://seich.martianwabbit.com/wp-content/uploads/2009/08/preview.jpg"><img class="alignnone size-full wp-image-150" title="preview" src="http://seich.martianwabbit.com/wp-content/uploads/2009/08/preview.jpg" alt="preview" width="200" height="200" /></a></p>
<p>PHP is a very versatile scripting language. In this tutorial I will be explaining how to create a PHP class that will help you post and get information from a twitter account using the client URL library (cURL).</p>
<p><span id="more-149"></span></p>
<p><strong>Tutorial Details</strong></p>
<ul>
<li><strong>Program:</strong> PHP/Twitter API</li>
<li><strong>Version:</strong> 1</li>
<li><strong>Difficulty:</strong> Easy</li>
<li><strong>Estimated Completion Time:</strong> 10 minutes</li>
</ul>
<h3>Step 1 &#8211; Starting up</h3>
<p>Let&#8217;s begin. First, we will create a file named twit.php which will contain our class. Once created, we declare our class and name it twit.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">class</span> twit <span style="color: #009900;">&#123;</span><span style="color: #666666; font-style: italic;">//define class &quot;twit&quot;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<h3>Step 2 &#8211; The Constructor</h3>
<p>We will now add our class&#8217;s public variables and its constructor. The constructor is a special function that will be run when we create a new instance of the class.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">class</span> twit <span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$password</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$password</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>Now we have defined the two variables that we will be using throughout the class. These will be defined when a new instance of the class is created, thanks to our __construct method.</p>
<h3>Step 3 &#8211; Posting</h3>
<p>It&#8217;s time to define our first method.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">class</span> twit <span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$password</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$password</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">function</span> postStatus<span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweet</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_init"><span style="color: #990000;">curl_init</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//initiate curl session.</span><br />
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #0000ff;">'http://www.twitter.com/statuses/update.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//set URL to connect to.</span><br />
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_CONNECTTIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POST<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//set mode to POST.</span><br />
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;status=&quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/urlencode"><span style="color: #990000;">urlencode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweet</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//send status information.</span><br />
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_USERPWD<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$this-&gt;username</span>:<span style="color: #006699; font-weight: bold;">$this-&gt;password</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//send login information.</span><br />
<br />
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_exec"><span style="color: #990000;">curl_exec</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//execute curl session.</span><br />
<span style="color: #000088;">$http_status</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_getinfo"><span style="color: #990000;">curl_getinfo</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLINFO_HTTP_CODE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//get response code.</span><br />
<br />
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$http_status</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #666666; font-style: italic;">//200 means the post was succesful.</span><br />
<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<a href="http://www.php.net/curl_close"><span style="color: #990000;">curl_close</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//close curl connection.</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>The postStatus method will be used to tweet directly to our account, to achieve this we use the cURL to post using twitter&#8217;s api. The setup is simple and configured as specified on the twitter api page. The method returns true or false based upon the response code sent back from twitter this, makes the class easier to be used within another page in order to display success/error messages or trigger some other action.</p>
<p>Let&#8217;s try our class and see what we have so far.<br />
Create another php file and try the following code in it. Make sure to replace USER and PASSWORD with your twitter username and password.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;twit.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$twitter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> twit<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;USER&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;PASSWORD&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//replace these</span><br />
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$twitter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">postStatus</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Test!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Status Posted!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;There was an error when posting the status to twitter.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>If everything is working correctly you should see the post on your twitter account and it should appear as posted through the API.</p>
<p><a href="http://seich.martianwabbit.com/wp-content/uploads/2009/08/1.jpg"><img class="alignnone size-full wp-image-156" title="1" src="http://seich.martianwabbit.com/wp-content/uploads/2009/08/1.jpg" alt="1" width="600" height="80" /></a></p>
<h3>Step 4 &#8211; Getting</h3>
<p>Finally, we will define our second and last method which, is going to help us get the information of our last twitter status.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">class</span> twit <span style="color: #009900;">&#123;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$password</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">password</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$password</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">function</span> postStatus<span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweet</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_init"><span style="color: #990000;">curl_init</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #0000ff;">'http://www.twitter.com/statuses/update.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_CONNECTTIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POST<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;status=&quot;</span><span style="color: #339933;">.</span><a href="http://www.php.net/urlencode"><span style="color: #990000;">urlencode</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweet</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_USERPWD<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$this-&gt;username</span>:<span style="color: #006699; font-weight: bold;">$this-&gt;password</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_exec"><span style="color: #990000;">curl_exec</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$http_status</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_getinfo"><span style="color: #990000;">curl_getinfo</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLINFO_HTTP_CODE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$http_status</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<a href="http://www.php.net/curl_close"><span style="color: #990000;">curl_close</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">function</span> getStatus<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_init"><span style="color: #990000;">curl_init</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #0000ff;">'http://twitter.com/statuses/user_timeline/'</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//set url to the user's timeline.</span><br />
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_VERBOSE<span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_USERPWD<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$this-&gt;username</span>:<span style="color: #006699; font-weight: bold;">$this-&gt;password</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_HTTP_VERSION<span style="color: #339933;">,</span> CURL_HTTP_VERSION_1_1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_CONNECTTIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/curl_setopt"><span style="color: #990000;">curl_setopt</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_GET<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//set mode to get.</span><br />
<br />
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_exec"><span style="color: #990000;">curl_exec</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$http_status</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/curl_getinfo"><span style="color: #990000;">curl_getinfo</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLINFO_HTTP_CODE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<a href="http://www.php.net/curl_close"><span style="color: #990000;">curl_close</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$http_status</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000088;">$resultArray</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/simplexml_load_string"><span style="color: #990000;">simplexml_load_string</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//load the result into a simplexml object.</span><br />
<span style="color: #b1b100;">return</span> <span style="color: #000088;">$resultArray</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">status</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//return object.</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>This time we will be using the cURL&#8217;s get option, which will allow us to get the information from the twitter server.<br />
In this method I am using a simplexml object to make the task of managing information easier, since we will be receiving a lot of data from the server.</p>
<p>Let&#8217;s try the new method out! Create a new PHP file and insert the following code into it. Once again, make sure to replace USER and PASSWORD with your corresponding twitter username and password.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;twit.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$twitter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> twit<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;USER&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;PASSWORD&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><span style="color: #666666; font-style: italic;">//replace these</span><br />
<span style="color: #000088;">$tweeter2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tweeter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getStatus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<a href="http://www.php.net/var_dump"><span style="color: #990000;">var_dump</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweeter2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<div class="tutorial_image"><a href="http://seich.martianwabbit.com/wp-content/uploads/2009/08/2.jpg"><img class="alignnone size-full wp-image-157" title="2" src="http://seich.martianwabbit.com/wp-content/uploads/2009/08/2.jpg" alt="2" width="600" height="213" /></a></div>
<p>You will get a lot of data, but since it&#8217;s stored in a simplexml object it is quite easy to manipulate it.<br />
For example, if you wanted to get the text from your last post you could use:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;twit.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$tweeter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> twit<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Seich&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweeter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getStatus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>Which would echo whatever you wrote in your last update. Using this same method you can access all of the data provided.</p>
<h3>Conclusion</h3>
<p>Twitter&#8217;s API is quite easy to manipulate. It is a very repetitive task though, so an object-oriented approach can help us making the process easier. Using these two methods as example, you should be able to expand the class or create your own approach to it. Nevertheless you should now be able to play with the Twitter API and manipulate Twitter&#8217;s data.</p>
<h3>Further reading</h3>
<p>I would recommend reading these in order to get a deeper view of what we covered in this tutorial.</p>
<ul>
<li><a href="http://php.net/curl">PHP Client URL Library Manual</a></li>
<li><a href="http://apiwiki.twitter.com/">Twitter API Wiki</a></li>
<li><a href="http://php.net/simplexml">PHP SimpleXML Object Manual</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://seich.martianwabbit.com/building-a-simple-twitter-php-class/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
