<?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>PHP &#8211; James Lin&#039;s Blog</title>
	<atom:link href="https://james.lin.net.nz/category/technology/programming/php/feed/" rel="self" type="application/rss+xml" />
	<link>https://james.lin.net.nz</link>
	<description>Just bits and pieces of my life</description>
	<lastBuildDate>Fri, 19 Jan 2018 22:42:50 +0000</lastBuildDate>
	<language>en-NZ</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.9.2</generator>
<site xmlns="com-wordpress:feed-additions:1">22801464</site>	<item>
		<title>Laravel for Django developers, part 3</title>
		<link>https://james.lin.net.nz/2018/01/20/laravel-for-django-developers-part-3/</link>
		<comments>https://james.lin.net.nz/2018/01/20/laravel-for-django-developers-part-3/#respond</comments>
		<pubDate>Fri, 19 Jan 2018 22:25:30 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=3992</guid>
		<description><![CDATA[During the last day of Christmas break I&#8217;ve spent a few hours trying to pick up Laravel again, from the beginning. I remember I blogged about Laravel back in 2016 (time flies!), and reading them again, I am surprised that what I thought back in 2016 still applies today. Today I am going to talk… <span class="read-more"><a href="https://james.lin.net.nz/2018/01/20/laravel-for-django-developers-part-3/">Read More &#187;</a></span>]]></description>
				<content:encoded><![CDATA[<p>During the last day of Christmas break I&#8217;ve spent a few hours trying to pick up Laravel again, from the beginning.</p>
<p>I remember I blogged about Laravel back in 2016 (time flies!), and reading them again, I am surprised that what I thought back in 2016 still applies today.</p>
<p>Today I am going to talk about some black magic I&#8217;ve encountered writing a test blog project. The bit that filling out the blog form and validating the data. Normally, when a form is posted to an URL, the controller validates the form data and throws validation exceptions if form is not validated, then the controller can redirect the browser back to the form along some error data to be displayed on the form. In Laravel, the redirecting back to the form is automatically done for you and somehow the view magically has the &#8216;errors&#8217; variable in context.</p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2018/01/20/laravel-for-django-developers-part-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">3992</post-id>	</item>
		<item>
		<title>Writing static typed style code in dynamic type language</title>
		<link>https://james.lin.net.nz/2016/02/22/writing-static-typed-style-code-in-dynamic-type-language/</link>
		<comments>https://james.lin.net.nz/2016/02/22/writing-static-typed-style-code-in-dynamic-type-language/#respond</comments>
		<pubDate>Sun, 21 Feb 2016 20:15:25 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=3755</guid>
		<description><![CDATA[I am not sure who else is doing this but I have a static typed language background and I tend to write code in static type style. Don&#8217;t assign different value types to the same variable Let&#8217;s see what dynamic type in the simplest form in Python: [crayon-5a65e10d0240e659064369/] While this is perfectly legitimate, but it&#8217;s… <span class="read-more"><a href="https://james.lin.net.nz/2016/02/22/writing-static-typed-style-code-in-dynamic-type-language/">Read More &#187;</a></span>]]></description>
				<content:encoded><![CDATA[<p>I am not sure who else is doing this but I have a static typed language background and I tend to write code in static type style.</p>
<h3>Don&#8217;t assign different value types to the same variable</h3>
<p>Let&#8217;s see what dynamic type in the simplest form in Python:</p><pre class="crayon-plain-tag">variable_a = True
if variable_a:
    variable_a = 'Result'</pre><p>While this is perfectly legitimate, but it&#8217;s perfectly unacceptable, the rule of thumb is, you never assign a different type of value to a already defined variable.</p><pre class="crayon-plain-tag">variable_a = True
if variable_a:
    result = 'Result'</pre><p>This way, when someone reads your code, one can safely assume result is always a &#8216;String&#8217; type, seriously this will help a lot.</p>
<h3>Don&#8217;t return different types of value in a function</h3>
<p>Consider the following example, one would need to understand your condition in order to determine the type of result, also makes it difficult to consume this function.</p><pre class="crayon-plain-tag">def work():
    result = None
    variable_a = True
    if variable_a:
        result = 'Result'
    else
        result = 123</pre><p></p>
<h3>Define your class members at the top of the class</h3>
<p>While this is perfectly legitimate:</p><pre class="crayon-plain-tag">class A(object):
    def work(self):
        self.member = 'abc'</pre><p>But it&#8217;s difficult to work out what class members at a glance, please define them.</p><pre class="crayon-plain-tag">class A(object):
    def __init__(self):
        self.member = None

    def work(self):
        self.member = 'abc'</pre><p>Now you know this class has a class member without reading the work function.</p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2016/02/22/writing-static-typed-style-code-in-dynamic-type-language/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">3755</post-id>	</item>
		<item>
		<title>PHP developers should try Python.</title>
		<link>https://james.lin.net.nz/2016/02/21/php-developers-should-try-python/</link>
		<comments>https://james.lin.net.nz/2016/02/21/php-developers-should-try-python/#respond</comments>
		<pubDate>Sun, 21 Feb 2016 03:26:51 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=3745</guid>
		<description><![CDATA[PHP and Python both are dynamic typed language, so there is no point the discuss anything related in this aspect. Exception is first class citizen [crayon-5a65e10d03aa6769181499/] Prints &#8216;Caught&#8217; Let&#8217;s look at PHP: [crayon-5a65e10d03abc872044553/] Better functions Imagine you can write functions like this [crayon-5a65e10d03ac8361203662/] Short-lived vs Long-lived Almost all PHP scripts are short-lived, including most frameworks, which… <span class="read-more"><a href="https://james.lin.net.nz/2016/02/21/php-developers-should-try-python/">Read More &#187;</a></span>]]></description>
				<content:encoded><![CDATA[<p>PHP and Python both are dynamic typed language, so there is no point the discuss anything related in this aspect.</p>
<h3>Exception is first class citizen</h3>
<p></p><pre class="crayon-plain-tag">try:
    a = 1 / 0
except:
    print 'Caught'</pre><p>Prints &#8216;Caught&#8217;</p>
<p>Let&#8217;s look at PHP:</p><pre class="crayon-plain-tag">try{ 
    $a = 1 / 0; 
}catch (Exception $e){ 
    print &quot;Caught&quot;; 
} 
I cannot believe by default this still gives me PHP Warning:  Division by zero</pre><p></p>
<h3>Better functions</h3>
<p>Imagine you can write functions like this</p><pre class="crayon-plain-tag">def marry(man, woman):
    ...

marry('John', 'Julie')
marry(woman='Julie', man='John')

def party(*args):
    for person in args:
      print person

party('John')
party('John', 'Ben', ...)

def save(name, *kwargs):
    data = {'name': name}
    data.update(kwargs)
    write_to_db(data)

save('John')
save('John', gender='male', birthday='01/01/1970')</pre><p></p>
<h3>Short-lived vs Long-lived</h3>
<p>Almost all PHP scripts are short-lived, including most frameworks, which means to serve a request, your PHP application will run from start to finish. However, Python can run as processes to serve requests, for example, A Django application runs as a process, so initiation code only runs once, serve a request, then wait for the next request to serve.</p>
<h3>Imports Properly</h3>
<p>Since 5.3, PHP introduced namespace, since they used .&#8221;dot&#8221; for concatenation, so they had to use the \&#8221;slash&#8221;, looks like this</p><pre class="crayon-plain-tag">namespace NZ\NET\LIN\Test</pre><p>And use like this</p><pre class="crayon-plain-tag">use NZ\NET\LIN\Test</pre><p></p>
<p>But it doesn&#8217;t actually import the Test class! Unless you use __autoload!</p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2016/02/21/php-developers-should-try-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">3745</post-id>	</item>
		<item>
		<title>Laravel for Django developers, part 1</title>
		<link>https://james.lin.net.nz/2016/02/05/laravel-for-django-developers-part-1/</link>
		<comments>https://james.lin.net.nz/2016/02/05/laravel-for-django-developers-part-1/#respond</comments>
		<pubDate>Fri, 05 Feb 2016 00:05:27 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Laravel]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=3699</guid>
		<description><![CDATA[Project Structure After setting up the quickstart project, I feel the project structure is quite different to Django. In Django a lot of code are organised in &#8220;app&#8221; domain, but it seems to me that multi-app is not something out-of-the-box in Laravel: https://laracasts.com/discuss/channels/general-discussion/laravel-5-multi-app-or-modular-applications Instead, it seems the apps in Django is more similar to the… <span class="read-more"><a href="https://james.lin.net.nz/2016/02/05/laravel-for-django-developers-part-1/">Read More &#187;</a></span>]]></description>
				<content:encoded><![CDATA[<h1>Project Structure</h1>
<p>After setting up the <a href="https://laravel.com/docs/5.2/quickstart" target="_blank">quickstart</a> project, I feel the project structure is quite different to Django. In Django a lot of code are organised in &#8220;app&#8221; domain, but it seems to me that multi-app is not something out-of-the-box in Laravel: <a href="https://laracasts.com/discuss/channels/general-discussion/laravel-5-multi-app-or-modular-applications">https://laracasts.com/discuss/channels/general-discussion/laravel-5-multi-app-or-modular-applications</a></p>
<p>Instead, it seems the apps in Django is more similar to the &#8220;Service Providers&#8221; in Laravel.</p>
<h1>ORM</h1>
<p>While working on the <a href="https://laravel.com/docs/5.2/quickstart" target="_blank">quickstart</a> project in Laravel, it seems to me that Eloquent doesn&#8217;t generate initial migratios based on model definition.</p>
<p>In Django, you define the model, then by running &#8220;makemigration&#8221; CLI command, it will generate initial migration files to create the database table for that model.</p>
<p>According to the quickstart project, it seems to be things work the other way around, you will have to manually create the migration files by running</p><pre class="crayon-plain-tag">php artisan make:migration create_tasks_table --create=tasks</pre><p>And then create the model class by running</p><pre class="crayon-plain-tag">php artisan make:model Task</pre><p></p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2016/02/05/laravel-for-django-developers-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">3699</post-id>	</item>
		<item>
		<title>Laravel for Django Developers, prelude.</title>
		<link>https://james.lin.net.nz/2016/02/05/laravel-for-django-developers-prelude/</link>
		<comments>https://james.lin.net.nz/2016/02/05/laravel-for-django-developers-prelude/#respond</comments>
		<pubDate>Thu, 04 Feb 2016 23:56:58 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Laravel]]></category>

		<guid isPermaLink="false">http://james.lin.net.nz/?p=3697</guid>
		<description><![CDATA[It appeared to me that &#8220;Laravel&#8221; is kind of similar to Django in the way of implementation concept, so this is my obvious choice. I did a google search &#8220;Laravel for Django developers&#8221; and got pretty much nothing useful, sure, who would have move back to PHP from Python? I think this only happens in… <span class="read-more"><a href="https://james.lin.net.nz/2016/02/05/laravel-for-django-developers-prelude/">Read More &#187;</a></span>]]></description>
				<content:encoded><![CDATA[<p>It appeared to me that &#8220;Laravel&#8221; is kind of similar to Django in the way of implementation concept, so this is my obvious choice.</p>
<p>I did a google search &#8220;Laravel for Django developers&#8221; and got pretty much nothing useful, sure, who would have move back to PHP from Python? I think this only happens in New Zealand.</p>
<p>In the coming series, I will share my journey of learning Laravel, while comparing to Django, hopefully it will help out other Django developers like myself.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2016/02/05/laravel-for-django-developers-prelude/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">3697</post-id>	</item>
		<item>
		<title>PHP Snippet: Get dates from a given range</title>
		<link>https://james.lin.net.nz/2011/03/08/php-snippet-get-dates-from-a-given-range/</link>
		<comments>https://james.lin.net.nz/2011/03/08/php-snippet-get-dates-from-a-given-range/#respond</comments>
		<pubDate>Mon, 07 Mar 2011 20:01:08 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://james.limsbros.com/?p=602</guid>
		<description><![CDATA[[crayon-5a65e10d04d4c916166550/] Result: [crayon-5a65e10d04d66507170583/] ﻿]]></description>
				<content:encoded><![CDATA[<p></p><pre class="crayon-plain-tag">function get_range_dates($start, $end){
  $days = array();
  $start_tmp = explode(&quot;-&quot;,$start);
  $end_tmp = explode(&quot;-&quot;,$end);
  $start_time = mktime(1,1,1, $start_tmp[1], $start_tmp[2], $start_tmp[0]);
  $end_time = mktime(1,1,1, $end_tmp[1], $end_tmp[2], $end_tmp[0]);       
  while($start_time &amp;lt;= $end_time){           
    $days[] = date(&quot;Y-m-d&quot;, $start_time);
    $start_time += 86400;
  }
  return $days;
}

//calling the function
get_range_dates('2011-02-01', '2011-03-08');</pre><p></p>
<p>Result:</p><pre class="crayon-plain-tag">Array ( [0] =&gt; 2011-02-01 [1] =&gt; 2011-02-02 [2] =&gt; 2011-02-03 [3] =&gt; 2011-02-04 [4] =&gt; 2011-02-05 [5] =&gt; 2011-02-06 [6] =&gt; 2011-02-07 [7] =&gt; 2011-02-08 [8] =&gt; 2011-02-09 [9] =&gt; 2011-02-10 [10] =&gt; 2011-02-11 [11] =&gt; 2011-02-12 [12] =&gt; 2011-02-13 [13] =&gt; 2011-02-14 [14] =&gt; 2011-02-15 [15] =&gt; 2011-02-16 [16] =&gt; 2011-02-17 [17] =&gt; 2011-02-18 [18] =&gt; 2011-02-19 [19] =&gt; 2011-02-20 [20] =&gt; 2011-02-21 [21] =&gt; 2011-02-22 [22] =&gt; 2011-02-23 [23] =&gt; 2011-02-24 [24] =&gt; 2011-02-25 [25] =&gt; 2011-02-26 [26] =&gt; 2011-02-27 [27] =&gt; 2011-02-28 [28] =&gt; 2011-03-01 [29] =&gt; 2011-03-02 [30] =&gt; 2011-03-03 [31] =&gt; 2011-03-04 [32] =&gt; 2011-03-05 [33] =&gt; 2011-03-06 [34] =&gt; 2011-03-07 [35] =&gt; 2011-03-08 )</pre><p>﻿</p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2011/03/08/php-snippet-get-dates-from-a-given-range/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">602</post-id>	</item>
		<item>
		<title>PHP Tutorial: Server load bar chart</title>
		<link>https://james.lin.net.nz/2010/12/24/php-tutorial-server-load-bar-chart/</link>
		<comments>https://james.lin.net.nz/2010/12/24/php-tutorial-server-load-bar-chart/#respond</comments>
		<pubDate>Thu, 23 Dec 2010 15:51:52 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://james.limsbros.com/?p=489</guid>
		<description><![CDATA[Server Load: Just a quick tutorial on drawing the server load bar chart in php: serverload.php [crayon-5a65e10d055e1706997203/] html [crayon-5a65e10d055f7998459637/]]]></description>
				<content:encoded><![CDATA[<p style="float:left;margin: 15px 8px 0 0;">Server Load:</p>
<p> <img src="http://james.lin.net.nz/serverload.php" style="float:left;border:1px solid black;"/></p>
<div style="clear:both;"></div>
<p>Just a quick tutorial on drawing the server load bar chart in php:</p>
<p>serverload.php</p><pre class="crayon-plain-tag">function get_server_load($windows = 0)
{
    $os = strtolower(PHP_OS);
    if (strpos($os, &quot;win&quot;) === false)
    {
        if (file_exists(&quot;/proc/loadavg&quot;))
        {
            $load = file_get_contents(&quot;/proc/loadavg&quot;);
            $load = explode(' ', $load);
            return $load;
        }
        elseif (function_exists(&quot;shell_exec&quot;))
        {
            $load = explode(' ', `uptime`);
            return $load;
        }
        else
        {
            return &quot;&quot;;
        }
    }
}
list($one, $two, $three) = get_server_load();
//bar width
$width = 300;
//bar height
$height = 20;
$real_load = $one / 2;
$img_handle = imagecreate ($width, $height);
$box_color = imagecolorallocate ($img_handle, 255, 255, 255);
$bar_color = imagecolorallocate ($img_handle, 0, 195, 255);
$text_color = imagecolorallocate ($img_handle, 0, 0, 0);
$real_load = $real_load &gt; 1 ? 1 : $real_load;
imagerectangle ( $img_handle , 0 , 0 , $width, $height, $box_color);
imagefilledrectangle ( $img_handle , 0 , 0 , (int)$width*$real_load, $height, $bar_color);
imagestring($img_handle, 5, $width/2-15, $height/2-7, ($real_load*100).'%', $text_color);
header (&quot;Content-type: image/png&quot;);
imagepng ($img_handle);</pre><p></p>
<p>html</p><pre class="crayon-plain-tag">&lt;img src=&quot;serverload.php&quot; style=&quot;border:1px solid black;&quot;/&gt;</pre><p></p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2010/12/24/php-tutorial-server-load-bar-chart/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">489</post-id>	</item>
		<item>
		<title>My dokuwiki plugins</title>
		<link>https://james.lin.net.nz/2010/10/15/my-dokuwiki-plugins/</link>
		<comments>https://james.lin.net.nz/2010/10/15/my-dokuwiki-plugins/#comments</comments>
		<pubDate>Thu, 14 Oct 2010 18:25:17 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://james.limsbros.com/?p=353</guid>
		<description><![CDATA[A while ago I wrote some dokuwiki plugins: User Home Page Auto Include Index Table Math Modal Popup ACL Made Easy]]></description>
				<content:encoded><![CDATA[<p>A while ago I wrote some dokuwiki plugins:</p>
<p><a href="http://www.dokuwiki.org/plugin:userhomepage">User Home Page</a></p>
<p><a href="http://www.dokuwiki.org/plugin:autoincludeindex">Auto Include Index</a></p>
<p><a href="http://www.dokuwiki.org/plugin:tablemath">Table Math</a></p>
<p><a href="http://www.dokuwiki.org/plugin:modalpopup">Modal Popup</a></p>
<p><a href="http://www.dokuwiki.org/plugin:aclmadeeasy">ACL Made Easy</a></p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2010/10/15/my-dokuwiki-plugins/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">353</post-id>	</item>
		<item>
		<title>How do you rotate a two dimensional array?</title>
		<link>https://james.lin.net.nz/2010/07/28/how-do-you-rotate-a-two-dimensional-array/</link>
		<comments>https://james.lin.net.nz/2010/07/28/how-do-you-rotate-a-two-dimensional-array/#respond</comments>
		<pubDate>Wed, 28 Jul 2010 01:29:25 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://james-lin.no-ip.org/?p=45</guid>
		<description><![CDATA[http://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array This is the best so far I have come up with PHP, can anyone refine this for me?? [crayon-5a65e10d05fdb294516183/]]]></description>
				<content:encoded><![CDATA[<div><a href="http://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array">http://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array</a></div>
<div>This is the best so far I have come up with PHP, can anyone refine this for me??</div>
<p></p><pre class="crayon-plain-tag">$b = array();
$a = array(array(1,2,3,4),array(5,6,7,8),array(9,0,1,2),array(3,4,5,6));

while(count($a)&gt;0)
{
    $b[count($a[0])-1][] = array_shift($a[0]);
    if (count($a[0])==0)
    {
         array_shift($a);
    }
}</pre><p></p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2010/07/28/how-do-you-rotate-a-two-dimensional-array/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">45</post-id>	</item>
		<item>
		<title>Tutorial &#8211; Twitter API using OAuth Single Access Token</title>
		<link>https://james.lin.net.nz/2010/07/28/tutorial-twitter-api-using-oauth-single-access-token/</link>
		<comments>https://james.lin.net.nz/2010/07/28/tutorial-twitter-api-using-oauth-single-access-token/#respond</comments>
		<pubDate>Wed, 28 Jul 2010 01:23:11 +0000</pubDate>
		<dc:creator><![CDATA[James Lin]]></dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://james-lin.no-ip.org/?p=35</guid>
		<description><![CDATA[Hello guys, In this blog I am going to show you step by step how to retrieve and reply tweets using a single twitter account. I was asked to write up a service to monitor tweets of a twitter account, then I went to the twitter API to read up how to get this done,… <span class="read-more"><a href="https://james.lin.net.nz/2010/07/28/tutorial-twitter-api-using-oauth-single-access-token/">Read More &#187;</a></span>]]></description>
				<content:encoded><![CDATA[<p>Hello guys,</p>
<div>In this blog I am going to show you step by step how to retrieve and reply tweets using a single twitter account.</div>
<div>I was asked to write up a service to monitor tweets of a twitter  account, then I went to the twitter API to read up how to get this done,  what I found out is, in June 2010, twitter will be shutting down their  plain authentication method, which you cannot pass in twitter account  credential on API requests. Instead they are going to follow the OAuth (<a href="http://apiwiki.twitter.com/OAuth-FAQ">http://apiwiki.twitter.com/OAuth-FAQ</a>)  pattern. To summarize the whole OAuth concept in a sentence: your  twitter application will redirect the user to login to a twitter page,  the user will click on a button to authorize your application to use the  user&#8217;s information. Since I am writting a twitter bot, so it shouldn&#8217;t  login to a twitter page to authorise my twitter application. Luckly  twitter provides &#8220;Single Access Token&#8221; method (<a href="http://dev.twitter.com/pages/oauth_single_token">http://dev.twitter.com/pages/oauth_single_token</a>),  what this does is you login to twitter by using the bot&#8217;s account  credential, authorize your own application, then you get the access  token which your application can use to interact with twitter API.</div>
<div>1. Create an application on twitter <a href="http://dev.twitter.com/apps/new"><span style="text-decoration: underline;"><span style="color: #0000ff;">http://dev.twitter.com/apps/new</span></span></a></div>
<div>2. Once you have created an application, view the details, take  note of the application key and secret, and you should see a link on the  right hand side called &#8220;My Access Token&#8221;, click on it. This page gives  you your single access token of the twitter account that you created the  application with. And you will be using these tokens in your  application.</div>
<div>3. Download Abraham&#8217;s oAuth library <a href="http://twitteroauth.labs.poseurtech.com/connect.php">http://twitteroauth.labs.poseurtech.com/connect.php</a> you will be using this library to authencate and consume the twitter API</div>
<div>This is the code example for getting tweets</div>
<p></p><pre class="crayon-plain-tag">//use the Abraham's oAuth library
require_once 'twitteroauth/twitteroauth.php';

//replace the followng string
$key = 'key_string';
$secret = 'secret_string';
$oauth_token = 'auth_token_string';
$oauth_token_secret = 'auth_token_secret_string';

//the connection to twitter API
$connection = new TwitterOAuth($key, $secret, $oauth_token, $oauth_token_secret);

$params = array('count'=&amp;gt;200); // this is optional, gets 200 tweets
$tweets = $connection-&amp;gt;get(&quot;statuses/replies&quot;, $params);

//see the object layout
print_r($tweets);</pre><p></p>
]]></content:encoded>
			<wfw:commentRss>https://james.lin.net.nz/2010/07/28/tutorial-twitter-api-using-oauth-single-access-token/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<post-id xmlns="com-wordpress:feed-additions:1">35</post-id>	</item>
	</channel>
</rss>
