<?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>The Lylo Files &#187; php</title>
	<atom:link href="http://blog.lylo.co.uk/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.lylo.co.uk</link>
	<description>Not very much at all, really</description>
	<lastBuildDate>Mon, 15 Mar 2010 22:56:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to display Twitter updates using PHP (and without using curl)</title>
		<link>http://blog.lylo.co.uk/2009/02/04/how-to-display-twitter-updates-using-php-and-without-using-curl/</link>
		<comments>http://blog.lylo.co.uk/2009/02/04/how-to-display-twitter-updates-using-php-and-without-using-curl/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 16:56:24 +0000</pubDate>
		<dc:creator>Olly</dc:creator>
				<category><![CDATA[web]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.lylo.co.uk/?p=96</guid>
		<description><![CDATA[
This is something I needed to do recently.  The following code reads a user timeline from Twitter (I&#8217;ve chosen the RSS format) and puts the result into a &#60;ul&#62; block.  I&#8217;ve added a bunch of comments since PHP isn&#8217;t what I&#8217;d call a self-explanatory language.


&#60;?php
$doc = new DOMDocument&#40;&#41;;
&#160;
# load the RSS -- replace [...]]]></description>
			<content:encoded><![CDATA[<p>
This is something I needed to do recently.  The following code reads a user timeline from Twitter (I&#8217;ve chosen the RSS format) and puts the result into a <code>&lt;ul&gt;</code> block.  I&#8217;ve added a bunch of comments since PHP isn&#8217;t what I&#8217;d call a self-explanatory language.
</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$doc</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DOMDocument<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># load the RSS -- replace 'lylo' with your user of choice
</span><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$doc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">load</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://twitter.com/statuses/user_timeline/lylo.rss'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;ul&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># number of &lt;li&gt; elements to display.  20 is the maximum
</span>  <span style="color: #000088;">$max_tweets</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>    
&nbsp;
  <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$doc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'item'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;"># fetch the title from the RSS feed. 
</span>    <span style="color: #666666; font-style: italic;"># Note: 'pubDate' and 'link' are also useful (I use them in the sidebar of this blog)
</span>    <span style="color: #000088;">$tweet</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeValue</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># the title of each tweet starts with &quot;username: &quot; which I want to remove
</span>    <span style="color: #000088;">$tweet</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweet</span><span style="color: #339933;">,</span> <span style="color: #990000;">stripos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tweet</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">':'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>   
&nbsp;
    <span style="color: #666666; font-style: italic;"># OPTIONAL: turn URLs into links
</span>    <span style="color: #000088;">$tweet</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@'</span><span style="color: #339933;">,</span> 
          <span style="color: #0000ff;">'&lt;a href=&quot;$1&quot;&gt;$1&lt;/a&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tweet</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># OPTIONAL: turn @replies into links
</span>    <span style="color: #000088;">$tweet</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/@([0-9a-zA-Z]+)/&quot;</span><span style="color: #339933;">,</span> 
          <span style="color: #0000ff;">&quot;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>http://twitter.com/<span style="color: #006699; font-weight: bold;">$1</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;@<span style="color: #006699; font-weight: bold;">$1</span>&lt;/a&gt;&quot;</span><span style="color: #339933;">,</span> 
          <span style="color: #000088;">$tweet</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;li&gt;&quot;</span><span style="color: #339933;">.</span> <span style="color: #000088;">$tweet</span>  <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/li&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">++</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$max_tweets</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/ul&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> 
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.lylo.co.uk/2009/02/04/how-to-display-twitter-updates-using-php-and-without-using-curl/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
