<?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>Views From The Hill &#187; API</title>
	<atom:link href="http://dustint.com/archives/tag/api/feed" rel="self" type="application/rss+xml" />
	<link>http://dustint.com</link>
	<description>Tales Of A (Former) SFU Computing Scientist</description>
	<lastBuildDate>Thu, 17 Jun 2010 20:14:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Build an API for any website with Web-Scraping</title>
		<link>http://dustint.com/archives/184</link>
		<comments>http://dustint.com/archives/184#comments</comments>
		<pubDate>Sun, 16 May 2010 07:00:21 +0000</pubDate>
		<dc:creator>Dustin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Screen Scraping]]></category>
		<category><![CDATA[Web Scraping]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://dustint.com/?p=184</guid>
		<description><![CDATA[There are a lot of web-sites out there, with a lot of data on them. Sometimes you are building a killer web-app and you just have to have some data off a certain site. The problem is, that particular site doesn&#8217;t have an API that you can just plug into! Never fear, using some simple [...]]]></description>
			<content:encoded><![CDATA[<p>There are a lot of web-sites out there, with a lot of data on them. Sometimes you are building a killer web-app and you just have to have some data off a certain site. The problem is, that particular site doesn&#8217;t have an API that you can just plug into! Never fear, using some simple tools, combined with the Zend Framework you can create your own web-scraping (screen-scraping) API in no time.</p>
<p>Before I continue, I should mention that, not only is it impolite, but quite possibly illegal to take the content of a third party site without the owners permission or knowledge.</p>
<p>To accomplish our task we need some open-source tools:</p>
<ul>
<li><a title="Zend Framework" href="http://framework.zend.com">Zend Framework</a> (Required)</li>
<li><a href="http://quixapp.com/">Quix</a> (Recommended)</li>
<li><a title="Dojo Toolkit" href="http://www.dojotoolkit.org/">Dojo Toolkit</a> (Recommended)</li>
</ul>
<p>Our basic strategy is this:</p>
<ol>
<li> Find a target page with some cool content</li>
<li> Find the CSS Selector (or X-path) of the element we want to scrape</li>
<li> Verify the selector using Dojo</li>
<li> Build a Zend_Service object to fetch the cool content to PHP</li>
</ol>
<h2>Step 1: Finding the cool page</h2>
<p>Say I am a developer and I really want to get the outward facing IP of the machine that my PHP script is running on. If there is a router or proxy in between my machine and the Internet, this can be non trivial. As a solution I decide to use the service <a href="http://www.whatismyip.com/">http://www.whatismyip.com/</a></p>
<h2>Step 2: Finding the CSS3 Selector for the element</h2>
<p>Its pretty obvious which element we want to get the selector for. If you are feeling clever you can simply read the code or use a DOM inspector to figure out what the query is on your own. However, selectorgaget.com provides a cool tool that will allow you to point and click to determine the appropraite selector. You can get a bookmarklet directly from their site, or you can get the much more powerful Quix bookmarklet which includes the selectorgaget and a bunch of other cool tools.</p>
<p>If you decided on Quix (I know I did), click on the bookmarklet and enter &#8220;sg&#8221; in the command box. The selectorgaget should load up and as you move your mouse around the screen it should highlight different DOM nodes. We want the top one, so click on the text &#8220;Your IP&#8230;&#8221; and, checking out the path, we see that it is a rather boring h1.</p>
<h2>Step 3: Validation via Dojo</h2>
<p>I like to validate the path before taking it over to Zend. I use a Dojo and firebug lite bookmarklet which injects a debugging version of dojo into any page via the AOL CDN. To add this bookmarket drag <a  href="javascript:(function(){%20var%20d=document;%20var%20h=d.getElementsByTagName('HEAD')[0];%20var%20s=d.createElement('script');%20s.type='text/javascript';%20s.src='http://o.aolcdn.com/dojo/1.4.0/dojo/dojo.xd.js.uncompressed.js';%20h.appendChild(s);%20})()">Inject Dojo</a> into your bookmarks toolbar. In the debugging console that pops up, enter dojo.query(&#8216;h1&#8242;); and you should see the h1 DOM element being returned.</p>
<h2>Step 4: Moving it all to PHP</h2>
<p>Now that we have successfully found our CSS3 selector path, we can move over to PHP and come up with a new Zend_Service component. Our approach will be to extend Zend_Service_Abstract, and implement some custom methods to preform the screen scrape.</p>
<p>The finished class looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> WhatsMyIpService <span style="color: #000000; font-weight: bold;">extends</span> Zend_Service_Abstract
<span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * The service endpoint.
	 * This is where Zend_Http_Client will navigate
	 * to fill service requests
	 * @var string
	 */</span>
	protected <span style="color: #000088;">$_endpoint</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://whatismyip.com'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * handle to the client
	 * @var Zend_Http_Client
	 */</span>
	protected <span style="color: #000088;">$_client</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_client <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">getHttpClient</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Method to get the external IP of the computer / server
	 * script is running on
	 * @return string
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getMyIp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//reset the client parameters, set the URL to whatismyip.com</span>
		<span style="color: #666666; font-style: italic;">//and actually preform the request</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_client
			<span style="color: #339933;">-&gt;</span><span style="color: #004000;">resetParameters</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
			<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setUri</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_endpoint<span style="color: #009900;">&#41;</span>
			<span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #009900;">&#40;</span>Zend_Http_Client<span style="color: #339933;">::</span><span style="color: #004000;">GET</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//check to make sure that the result isnt a HTTP error</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isError</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Client returned error: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		try<span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">//setup the query object with the result body (HTML page)</span>
			<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Dom_Query<span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBody</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
			<span style="color: #000088;">$domCollection</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'h1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>catch<span style="color: #009900;">&#40;</span>Zend_Dom_Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Error Loading Document: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//check to make sure the query return a result</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$domCollection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Cannot find DOM Element'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//get the titlestring from the nodevalue</span>
		<span style="color: #000088;">$titleString</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$domCollection</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</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;">//now we should have the content of h1 stored in the titleString</span>
		<span style="color: #666666; font-style: italic;">//it should read something like &quot;Your IP Address Is: xxx.xxx.xxx.xxx&quot;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//Now we will parse out the IP address using regular expressions</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/([\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3})/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$titleString</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
			throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Unable to parse IP from page'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I hope that the script is sufficiently commented, but to summarize:</p>
<p>In the constructor, we get an local instance of a Zend_Http_Client object. Clone() is used to prevent any other Zend_Service objects from polluting our client with their own requests.</p>
<p>In the getMyIp() method, we first setup and preform the request (using the fluid interface provided by Zend_Http_Client). Notice how we reset the parameters: In this case its not actually necessary as we aren&#8217;t passing any parameters, but it is good habit to get into in case in the future we expand this class to include GET or POST parameter passing.  </p>
<p>Next, we examine what the HTTP client has passed back to us. Hopefully, if nothing has gone wrong, it is a HTML string representation of the page at whatismyip.com. Some basic checks are preformed to ensure that no HTTP errors have occurred, and then we instantiate a Zend_Dom_Query object which provides both CSS  and Xpath selectors.</p>
<p>Finally, after running the CSS selector query, we check to ensure we got a DOM element back, get its value and parse out the IP using Regular expressions. Its pretty impressive what can be done in <70 lines of code with the Zend Framework.</p>
<p>To run this class, we could create a directory structure as follows:<br />
library <- Includes Zend framework<br />
WhatsMyIpService.php <- The above class<br />
getIp.php <- php file including the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">set_include_path</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span>PATH_SEPARATOR<span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #990000;">realpath</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/library'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">get_include_path</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/** Zend_Loader_Autoload **/</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'Zend/Loader/Autoloader.php'</span><span style="color: #339933;">;</span>
Zend_Loader_Autoloader<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFallbackAutoloader</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$service</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> WhatsMyIpService<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$service</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMyIp</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The first couple of lines gets the library on the include path, the second block sets up autoloading so we don&#8217;t have to manually include files, and the final lines instantiates the class and calls the method.</p>
]]></content:encoded>
			<wfw:commentRss>http://dustint.com/archives/184/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Translink Zend Framework API</title>
		<link>http://dustint.com/archives/66</link>
		<comments>http://dustint.com/archives/66#comments</comments>
		<pubDate>Wed, 16 Sep 2009 09:30:01 +0000</pubDate>
		<dc:creator>Dustin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Zext]]></category>

		<guid isPermaLink="false">http://dustint.com/?p=66</guid>
		<description><![CDATA[Translink is the local public transit provider for beautiful Vancouver, Canada. The system consists of Buses, Boats and Trains. Translink released an Iphone app some time ago that allows the lookup of bus information. Michael Weisman was kind enough to write about the &#8220;hidden&#8221; api that is used by the Iphone app to preform AJAX [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://translink.bc.ca" target="_blank">Translink</a> is the local public transit provider for beautiful Vancouver, Canada. The system consists of Buses, Boats and Trains. Translink released an Iphone app some time ago that allows the lookup of bus information. Michael Weisman was kind enough to <a href="http://www.mweisman.com/transit.html">write</a> about the &#8220;hidden&#8221; api that is used by the Iphone app to preform AJAX calls.<br />
<span id="more-66"></span></p>
<p>The Zend Framework based API that I have written duplicates the functionality described on Micheal&#8217;s page and exposes the API functionality to PHP applications.</p>
<p>Please keep in mind that as this is an unofficial API, Translink may not want their data being used without their permission. Please contact Translink before making use of this API on a production site.</p>
<h1>Example Usage</h1>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$translink</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zext_Service_Translink<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;">//Get the route directions for the 135</span>
<span style="color: #000088;">$directions</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$translink</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRouteDirections</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'135'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Zend_Debug<span style="color: #339933;">::</span><span style="color: #004000;">dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$directions</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Get the east-bound stops for the 135</span>
<span style="color: #000088;">$stops</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$translink</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRouteStops</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'135'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'east'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Zend_Debug<span style="color: #339933;">::</span><span style="color: #004000;">dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stops</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Get the details for an arbitrary stop</span>
Zend_Debug<span style="color: #339933;">::</span><span style="color: #004000;">dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$translink</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getStopDetails</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">51845</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//get the latitude and longitude for a stop</span>
Zend_Debug<span style="color: #339933;">::</span><span style="color: #004000;">dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$translink</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getStopLocation</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">51845</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//get the stops near a given latitude and longitude</span>
Zend_Debug<span style="color: #339933;">::</span><span style="color: #004000;">dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$translink</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLocationStops</span><span style="color: #009900;">&#40;</span><span style="color:#800080;">49.25344</span><span style="color: #339933;">,-</span><span style="color:#800080;">123.167895</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>Download &amp; Install</h1>
<p>Download the following file:<br />
<a href="http://dustint.com/wp-content/uploads/2009/08/Zext_Service_Translink.zip">Zext_Service_Translink</a><br />
Add it to your library path (same folder that Zend of the Zend framework resides in) if you have autoloading enabled.</p>
<p>I will try to respond to any problems in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://dustint.com/archives/66/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>BC Lottery Corporation API</title>
		<link>http://dustint.com/archives/93</link>
		<comments>http://dustint.com/archives/93#comments</comments>
		<pubDate>Wed, 02 Sep 2009 09:30:05 +0000</pubDate>
		<dc:creator>Dustin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Zext]]></category>

		<guid isPermaLink="false">http://dustint.com/?p=93</guid>
		<description><![CDATA[The British Columbia Lottery Corporation has an unpublished API that they use to pull data down for the flash application on their home page. The Zext PHP API exposes functionality to query the most resent winning numbers from the BCLC website, as well as retrieve current jackpot estimates for the main lotteries in this province. [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.bclc.com/">British Columbia Lottery Corporation</a> has an unpublished API that they use to pull data down for the flash application on their home page. The Zext PHP API exposes functionality to query the most resent winning numbers from the BCLC website, as well as retrieve current jackpot estimates for the main lotteries in this province.<br />
<span id="more-93"></span><br />
Again, as with most of the API&#8217;s I have been publishing lately, this API relies on undocumented and unsupported functionality which may break or become unavailable at any time. Furthermore, there are likely licensing issues involved with unauthorized use of BCLC data, so proceed with caution if you plan on using this API in a production app.</p>
<h1>Example Usage</h1>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$bclc</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zext_Service_BCLC<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;">//Get the results of the latest 649 results</span>
<span style="color: #000088;">$bclc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get649Results</span><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;">//get the estimated jackpot size for the latest 649 results</span>
<span style="color: #000088;">$bclc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get649Estimate</span><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;">//Get the 649 Extra numbers for the latest draw</span>
<span style="color: #000088;">$bclc</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get649ExtraResults</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>There are many other functions that the API exposes. Take a look through the source to view them all.</p>
<h1>Download &#038; Install</h1>
<p>Download the following file:<br />
<a href='http://dustint.com/wp-content/uploads/2009/08/Zext_Service_BCLC.zip'>Zext_Service_BCLC</a><br />
Add it to your library path (same folder that Zend of the Zend framework resides in) if you have autoloading enabled.</p>
<p>I will try to respond to any problems in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://dustint.com/archives/93/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Epguides.com Zend Framework API</title>
		<link>http://dustint.com/archives/53</link>
		<comments>http://dustint.com/archives/53#comments</comments>
		<pubDate>Thu, 20 Aug 2009 05:05:33 +0000</pubDate>
		<dc:creator>Dustin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://dustint.com/?p=53</guid>
		<description><![CDATA[Lately I have been writing Service API&#8217;s for Zext, my Zend Framework library extension. I plan on publishing some of the more useful ones over the next couple of months on my blog. The first of the series is the Epguides.com API. Epguides.com is a listing of TV show episode listings. This can be useful, [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I have been writing Service API&#8217;s for Zext, my Zend Framework library extension. I plan on publishing some of the more useful ones over the next couple of months on my blog. The first of the series is the Epguides.com API.</p>
<p><span id="more-53"></span>Epguides.com is a listing of TV show episode listings. This can be useful, for example, if you are downloading episodes from the internet and want to automatically name the files. Alternatively, if you are hosting a website this API could expose current episode listings for shows of your choice.</p>
<p>The API consists of three parts:</p>
<ol>
<li>Zext_Service_Epguides &#8211; The basic service class. Has one public method: <em>getShowObject($showName)</em></li>
<li>Zext_Service_Epguides_Show &#8211; The objet that is returned by <em>getShowObject($showName)</em></li>
<li>Zext_Service_Epguides_Episode &#8211; Individual episode objects that reside in the show object</li>
</ol>
<h1>Zext_Service_Epguides</h1>
<p>Fortunately, Epguides.com has a very regular naming scheme for their pages. The basic pattern is http://epguides.com/{<em>show_name</em>}/ where <em>show_name</em> is the name of the show, in lowercase, with whitespace and special characters removed. This can be accomplished using the following regex:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$showName</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/[^\w]/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$showName</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Rather unfortunately, epguides.com doesn&#8217;t follow this naming scheme for all shows. For example, &#8220;The Office&#8221; (North American Version) is called &#8220;office_us&#8221;. Therefore, when querying epguides.com using this API you want to be using the string that identifies the proper URL of the desired show. The API will generate the correct URL for shows that conform to the regular naming scheme, but you must use the epguides.com naming conventions for show names that are exceptions to the rules.</p>
<p>After the API generates the URL for a given show, it fetches the corresponding page from Epguides.com and parses it. The parsing is done by a combination of domnode selection using the DOMDocument object and regular expressions. Fell free to read the source for more details.</p>
<p>The parser returns an array which is then passed to the Zext_Service_Epguides_Show() object.</p>
<p>The Show and episode classes are fairly straight forward and mostly act as containers to provide some convenience functions. I hope you can get a fairly good idea of how to work with them by viewing the below usage example.</p>
<h1>Example Usage</h1>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$epguidesAPI</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zext_Service_Epguides<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;">//Gets a show object for The Office</span>
<span style="color: #000088;">$show</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$epguidesAPI</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getShowObject</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'office_us'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Print out all the episodes as strings.</span>
<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$show</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAllEpisodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$episode</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$episode</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Get the 10th episode of the 2nd season</span>
<span style="color: #000088;">$episode</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$show</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getEpisode</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h1>Download and Install</h1>
<p>Download the following file:<br />
<a href="http://dustint.com/wp-content/uploads/2009/08/Zext_Service_Epguides.zip">Zext_Service_Epguides</a><br />
Add it to your library path (same folder that Zend of the Zend framework resides in) if you have autoloading enabled.</p>
<p>I will try to respond to any problems in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://dustint.com/archives/53/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
