<?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>Alexey Zakhlestins blog &#187; twitter</title>
	<atom:link href="http://blog.milkfarmsoft.com/category/computers/web/twitter/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.milkfarmsoft.com</link>
	<description>Programming for Mac and Web</description>
	<lastBuildDate>Thu, 17 Mar 2011 21:32:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com" />
	<atom:link rel="hub" href="http://superfeedr.com/hubbub" />
			<item>
		<title>work with TwitterData in PHP</title>
		<link>http://blog.milkfarmsoft.com/2009/08/work-with-twitterdata-in-php/</link>
		<comments>http://blog.milkfarmsoft.com/2009/08/work-with-twitterdata-in-php/#comments</comments>
		<pubDate>Sun, 02 Aug 2009 19:36:35 +0000</pubDate>
		<dc:creator>indeyets</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://blog.milkfarmsoft.com/?p=106</guid>
		<description><![CDATA[I recently created a small library for working with twitterdata in PHP. Sources are available from GitHub. Twitter Data is a simple, open, semi-structured format for embedding machine-readable, yet human-friendly, data in Twitter messages. <span style="color:#777"> . . . &#8594; Read More: <a href="http://blog.milkfarmsoft.com/2009/08/work-with-twitterdata-in-php/">work with TwitterData in PHP</a></span>]]></description>
			<content:encoded><![CDATA[<p>I recently created a small library for working with <a href="http://twitterdata.org/" target="_blank">twitterdata</a> in PHP. Sources are available from GitHub: <a href="http://github.com/indeyets/php-twitterdata/tree/master">php-twitterdata</a></p>
<p>Here&#8217;s the description from Twitter Data site:</p>
<blockquote><p>
Twitter Data is a simple, open, semi-structured format for embedding machine-readable, yet human-friendly, data in <a href="http://twitter.com/">Twitter</a> messages. This data can then be transmitted, received, and interpreted in real time by powerful new kinds of applications built on the Twitter platform. Here is an example Twitter Data message:</p>
<p><code>I love the #twitterdata proposal! $vote +1</code></p>
<p>The part with the dollar sign, $vote +1, is a piece of data embedded using the Twitter Data format.
</p></blockquote>
<p>To use php-twitterdata library, <a href="http://github.com/indeyets/php-twitterdata/downloads">download</a> it into the sub-direcory in your project&#8217;s directory (or any other place you like) and init the autoloader:</p>
<div class="php" style="font-family: monospace; color: #000000;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br /><span style="color: #2500B9; font-weight: bold;">require</span> <span style="color: #ff0000;">&#8216;php-twitterdata/autoload.php&#8217;</span>;<br /><span style="color: #000000; font-weight: bold;">?&gt;</span></div>
<p>PHP API provided by php-twitterdata has 2 layers. On lower level, there are 3 classes: <a href="http://github.com/indeyets/php-twitterdata/blob/e59c94763f60deb9701a380101047d9cb6586245/TwitterData_Message.php#L12" target="_blank">TwitterData_Message</a>, <a href="http://github.com/indeyets/php-twitterdata/blob/e59c94763f60deb9701a380101047d9cb6586245/TwitterData_Frame.php#L12" target="_blank">TwitterData_Frame</a> and <a href="http://github.com/indeyets/php-twitterdata/blob/e59c94763f60deb9701a380101047d9cb6586245/TwitterData_Tuple.php#L12" target="_blank">TwitterData_Tuple</a> which correspond to parts of data-enabled twitter-message. Message can consist of several Frames, Frame can consist of several Tuples and a &#8220;subject&#8221;. Objects of each of these classes can be created programmatically and combined in hierarchy. Each of them has &#8220;magic&#8221; <a href="http://docs.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.tostring" target="_blank">__toString()</a> method, so, to export them as a valid twitter-message, you just need to put objects in string context. Something like this:</p>
<div class="php" style="font-family: monospace; color: #000000;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br /><span style="color: #0000ff;">$tuple</span> = <span style="color: #000000; font-weight: bold;">new</span> TwitterData_Tuple<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8216;foo&#8217;</span>, <span style="color: #ff0000;">&#8216;bar&#8217;</span><span style="color: #66cc66;">&#41;</span>;<br /><span style="color: #0000ff;">$frame</span> = <span style="color: #000000; font-weight: bold;">new</span> TwitterData_Frame<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8216;Hello, world!&#8217;</span>, <a style="text-decoration: none;" href="http://docs.php.net/array" target="_blank"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$tuple</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br /><a style="text-decoration: none;" href="http://docs.php.net/echo" target="_blank"><span style="color: #000066;">echo</span></a> <span style="color: #0000ff;">$frame</span>;<br /><span style="color: #000000; font-weight: bold;">?&gt;</span></div>
<p>and the result will be:<br />
<code><br />
Hello, world! $foo bar<br />
</code></p>
<p>If you need to parse Twitter Data messages you should use <a href="http://github.com/indeyets/php-twitterdata/blob/e59c94763f60deb9701a380101047d9cb6586245/TwitterData_Parser.php#L29" target="_blank">TwitterData_Parser</a> class. It is a SAX-style parser, with DOM-style export. If all you need is a hierarchy of objects, use it like this:</p>
<div class="php" style="font-family: monospace; color: #000000;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br /><span style="color: #0000ff;">$parser</span> = <span style="color: #000000; font-weight: bold;">new</span> TwitterData_Parser<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$text_from_twitter</span><span style="color: #66cc66;">&#41;</span>;<br /><span style="color: #0000ff;">$message</span> = <span style="color: #0000ff;">$parser</span>-&gt;<span style="color: #006600;">export</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// $message is object of TwitterData_Message class</span><br /><span style="color: #000000; font-weight: bold;">?&gt;</span></div>
<p>Otherwise, you can specify another handler-class as a second parameter of constructor. Such class has to implement <a href="http://github.com/indeyets/php-twitterdata/blob/e59c94763f60deb9701a380101047d9cb6586245/TwitterData_Parser.php#L12" target="_blank">TwitterData_Parser_CallbackInterface</a>.</p>
<p>Does it all sound too complex? No problem!</p>
<p>php-twitterdata library also includes high-level interface, which consists of 2 simple functions: <a href="http://github.com/indeyets/php-twitterdata/blob/e59c94763f60deb9701a380101047d9cb6586245/TwitterData.php#L14" target="_blank">TwitterData::array_to_TwitterData()</a> and <a href="http://github.com/indeyets/php-twitterdata/blob/e59c94763f60deb9701a380101047d9cb6586245/TwitterData.php#L19" target="_blank">TwitterData::TwitterData_to_array()</a>. First one converts associative array into the string, which can be inserted into twitter-message, and second one takes message-string received from twitter and returns associative array parsed out of tuples from the first frame of message (If you need to get all possible data from message, you will still need to use low-level API).</p>
<p>Example of high-level API:</p>
<div class="php" style="font-family: monospace; color: #000000;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />TwitterData::<span style="color: #006600;">TwitterData_to_array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8216;Hello, world! $foo bar&#8217;</span><span style="color: #66cc66;">&#41;</span>;<br /><span style="color: #808080; font-style: italic;">// array(&#8216;foo&#8217; =&gt; &#8216;bar&#8217;);</span></p>
<p><span style="color: #0000ff;">$message</span> = <span style="color: #ff0000;">&#8216;Hello, world! &#8216;</span>.TwitterData::<span style="color: #006600;">array_to_TwitterData</span><span style="color: #66cc66;">&#40;</span><a style="text-decoration: none;" href="http://docs.php.net/array" target="_blank"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&#8216;foo&#8217;</span> =&gt; <span style="color: #ff0000;">&#8216;bar&#8217;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br /><span style="color: #808080; font-style: italic;">// Hello, world! $foo bar</span><br /><span style="color: #000000; font-weight: bold;">?&gt;</span></div>
<p>Library has <a href="http://github.com/indeyets/php-twitterdata/tree/e5f7ae14a65ed00368e23750de1addaac20f5684/tests" target="_blank">unit-tests</a>, which cover all examples provided on Twitter Data site both on <a href="http://twitterdata.org/" target="_blank">Introduction</a> page and on <a href="http://twitterdata.org/examples/" target="_blank">Examples</a> page and is licensed under <a href="http://github.com/indeyets/php-twitterdata/blob/e5f7ae14a65ed00368e23750de1addaac20f5684/LICENSE#L1" target="_blank">MIT-style license</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.milkfarmsoft.com/2009/08/work-with-twitterdata-in-php/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

