<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>John Wright's Blog</title>
	
	<link>http://johnwright.me/blog</link>
	<description />
	<lastBuildDate>Mon, 06 Feb 2012 22:42:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/johnwright" /><feedburner:info uri="johnwright" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Adding Groups to Tank Auth for CodeIgniter</title>
		<link>http://feedproxy.google.com/~r/johnwright/~3/lHz06mBSke0/</link>
		<comments>http://johnwright.me/blog/tank-auth-groups-roles-admin/#comments</comments>
		<pubDate>Sun, 29 Jan 2012 00:06:37 +0000</pubDate>
		<dc:creator>John Wright</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[object oriented]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://johnwright.me/blog/?p=558</guid>
		<description><![CDATA[<a title="Tank Auth" href="http://www.konyukhov.com/soft/tank_auth/" target="_blank">Tank Auth by Ilya Konyukhov</a> is regarded as one of the best authentication libraries for <a title="CodeIgniter: PHP Open Source Web Application Framework" href="http://codeigniter.com/" target="_blank">CodeIgniter</a>. Tank Auth is the top recommendation on <a title="CodeIgniter authentication thread" href="http://stackoverflow.com/questions/346980/what-codeigniter-authentication-library-is-best" target="_blank">this landmark CodeIgniter authentication thread at stackoverflow</a> for it's rock solid security features. But one drawback of Tank Auth is that it has no support for groups or roles out of the box ('admins', 'users', etc). In this post we're going to add the most minimal groups/roles support to Tank Auth possible. In the spirit of clean and object oriented code principles, we won't change any code in the core Tank Auth library, only extend it with our own class. I'm not opposed to hacking things together, when it's a good fit for the goals of the project. But my goal for this extension of Tank Auth is to not touch any Tank Auth code at all and keep things as minimal as possible.]]></description>
			<content:encoded><![CDATA[<p><a class="pull-left" style="margin: 0 15px 5px 0;" href="http://en.wikipedia.org/wiki/Tank"><img class="alignleft size-thumbnail wp-image-578" title="Soviet T-34 tank column advancing near Leningrad, 1942" src="http://johnwright.me/blog/wp-content/uploads/2012/01/tank_auth-150x150.jpg" alt="Soviet T-34 tank column advancing near Leningrad, 1942" width="150" height="150" /><br />
Photo: Wikipedia</a> <a title="Tank Auth" href="http://www.konyukhov.com/soft/tank_auth/" target="_blank" rel="external nofollow">Tank Auth by Ilya Konyukhov</a> is regarded as one of the best authentication libraries for <a title="CodeIgniter: PHP Open Source Web Application Framework" href="http://codeigniter.com/" target="_blank" rel="external nofollow">CodeIgniter</a>. Tank Auth is the top recommendation on <a title="CodeIgniter authentication thread" href="http://stackoverflow.com/questions/346980/what-codeigniter-authentication-library-is-best" target="_blank" rel="external nofollow">this landmark CodeIgniter authentication thread at stackoverflow</a> for it&#8217;s rock solid security features. But one drawback of Tank Auth is that it has no support for groups or roles out of the box (&#8216;admins&#8217;, &#8216;users&#8217;, etc). In this post we&#8217;re going to add the most minimal groups/roles support to Tank Auth possible. In the spirit of clean and object oriented code principles, we won&#8217;t change any code in the core Tank Auth library, only extend it with our own class. I&#8217;m not opposed to hacking things together, when it&#8217;s a good fit for the goals of the project. But my goal for this extension of Tank Auth is to not touch any Tank Auth code at all and keep things as minimal as possible.</p>
<p>The approach outlined here makes a few assumptions.</p>
<ul>
<li>Tank Auth 1.0.9 installed in CodeIgniter 2.1.0</li>
<li>MySQL (5)</li>
<li>The default group ID will be &#8217;300&#8242;</li>
<li>The admin group ID will be &#8217;100&#8242;</li>
</ul>
<h4>Here is an overview of the steps&#8230;</h4>
<ol>
<li> Add a group/roles column to the users table in the database</li>
<li> Create a library in CodeIgniter that extends Tank Auth with groups/roles support</li>
<li> Create a model in CodeIgniter that extends the Tank Auth &#8216;users.php&#8217; model</li>
<li> Change everywhere the Tank Auth library is loaded to the new library</li>
</ol>
<p><span id="more-558"></span></p>
<h4>1. Add a group/roles column to the users table in the database</h4>
<p>This can be done using phpMyAdmin or whatever MySQL client you use.</p>
<p>You can run the following SQL code</p>
<script src="https://gist.github.com/1695824.js?file=1.sql" type="text/javascript"></script><noscript><div class="embed-github-gist-source"><code><pre>ALTER TABLE users ADD group_id INT NOT NULL DEFAULT '300'</pre></code></div></noscript>
<p>This will add a &#8216;group_id&#8217; column to users and give every user a default value of &#8217;300&#8242;</p>
<h4>2. Create a library in CodeIgniter that extends Tank Auth with groups/roles support</h4>
<p>(<a title="Tank_auth_groups.php" href="https://github.com/wrightlabs/tank_auth_groups/blob/blogpost/application/libraries/Tank_auth_groups.php" target="_blank" rel="external nofollow">View code on github</a>)<br />
Here we will extend the &#8216;Tank_auth&#8217; class, adding functions &#8216;is_group_member&#8217; and &#8216;is_admin&#8217;. We will also override the &#8216;login&#8217; function so we can add the &#8216;group_id&#8217; to the CodeIgniter session after we login. Having &#8216;group_id&#8217; in the session is necessary for the &#8216;is_group_member&#8217; and &#8216;is_admin&#8217; methods to work.</p>
<h4>3. Create a model in CodeIgniter that extends the Tank Auth &#8216;users.php&#8217; model</h4>
<p>(<a title="ta_groups_users.php" href="https://github.com/wrightlabs/tank_auth_groups/blob/blogpost/application/models/tank_auth/ta_groups_users.php" target="_blank" rel="external nofollow">View code on github</a>)<br />
Here we are extending the &#8216;Users&#8217; model class simply to add a function allowing us to set the group ID in code. Every user is &#8217;300&#8242; by default and this function can be used to change the users group within your app.</p>
<h4>4. Change everywhere the Tank Auth library is loaded to the new library</h4>
<p>This step is the only modification necessary to make all the previous changes in your app take effect. Find everywhere in your CodeIgniter app that you are loading &#8216;tank_auth&#8217; &#8230; <script src="https://gist.github.com/1695824.js?file=1.php" type="text/javascript"></script><noscript><div class="embed-github-gist-source"><code><pre>$this-&gt;load-&gt;library('tank_auth');
		</pre></code></div></noscript> And replace it by loading the &#8216;tank_auth_groups&#8217; library in the place of &#8216;tank_auth&#8217; &#8230; <script src="https://gist.github.com/1695824.js?file=2.php" type="text/javascript"></script><noscript><div class="embed-github-gist-source"><code><pre>$this-&gt;load-&gt;library('tank_auth_groups','','tank_auth');</pre></code></div></noscript></p>
<p>Out of the box, Tank Auth only loads the &#8216;tank_auth&#8217; library in one place, <a title="auth.php" href="https://github.com/wrightlabs/Tank-Auth/blob/blogpost/application/controllers/auth.php" target="_blank" rel="external nofollow">which is in the &#8216;Auth&#8217; controller</a>.</p>
<h4>Note about the admin group</h4>
<p>The &#8216;admin&#8217; group ID is hard coded as &#8217;100&#8242;. When setting up your app, you can manually modify a user, giving them a group ID of &#8217;100&#8242; to make them an admin. Then you could code an admin panel and give admins the ability to give users a group ID of &#8217;100&#8242; or any other group (using the &#8216;set_group_id&#8217; function added in step 3).</p>
<h4>Download and install</h4>
<p>With this download (<a title="tank_auth_groups.zip" href="https://github.com/wrightlabs/tank_auth_groups/zipball/blogpost" target="_blank" rel="external nofollow">tank_auth_groups.zip</a>) you can simple copy the files into your existing Tank Auth install on CodeIgniter. <strong>Then you will need to apply steps 1 and 4 to your project.</strong> From there you can code you&#8217;re app with this minimal groups support.</p>
<h4>Code</h4>
<p>The code is <a title="Tank Auth Groups Roles support (admin, users, etc)" href="https://github.com/wrightlabs/tank_auth_groups" target="_blank" rel="external nofollow">hosted on github</a> and I&#8217;ll keep this blog post in sync with the <a title="blogpost branch" href="https://github.com/wrightlabs/tank_auth_groups/tree/blogpost" target="_blank" rel="external nofollow">blogpost branch</a> of the git repository.</p>
<h4>Conclusion</h4>
<p>Tank Auth is a rock solid CodeIgniter authentication library. In this post we&#8217;ve gone over an approach to extend Tank Auth to add groups/roles and admin support. This extension was created with the goal of being as minimal as possible and not modifying any existing Tank Auth code if possible. By applying object oriented clean code principles to this extension we&#8217;ve been able to do pretty well to meet the goals in creating this extension.</p>
<h4>Additional Resources</h4>
<p>Admin base class:<br />
<script src="https://gist.github.com/1695824.js?file=admin_base.php" type="text/javascript"></script><noscript><div class="embed-github-gist-source"><code><pre>&lt;?php

// application/controllers/admin_base.php

class Admin_Base extends CI_Controller {
    
   function __construct()
   {
      parent::__construct();
                
      $this-&gt;load-&gt;library(
         'tank_auth_groups', '', 'tank_auth'
      );
                
      if(!$this-&gt;tank_auth-&gt;is_admin())
      {
         redirect('auth/login');
      }
   }
} 

?&gt;</pre></code></div></noscript><br />
Any controller that extends the Admin_Base class is an admin only controller (like this)<br />
<script src="https://gist.github.com/1695824.js?file=admin.php" type="text/javascript"></script><noscript><div class="embed-github-gist-source"><code><pre>&lt;?php

// application/controllers/admin.php

require 'admin_base.php';

class Admin extends Admin_Base {
    
    function index()
    {
        redirect('admin/dashboard');
    }

    function dashboard()
    {
        // display admin dashboard
    }
}

?&gt;</pre></code></div></noscript></p>
<h4>Also</h4>
<p><a title="Ion Auth" href="https://github.com/benedmunds/CodeIgniter-Ion-Auth" target="_blank" rel="external nofollow">Ion Auth by Ben Edmunds</a> is a popular CodeIgniter authentication library. I haven&#8217;t used it myself yet, but it does seem to offer significant groups, roles, admins etc features out of the box. It is also being actively developed (at of the time of writing). Definitely worth looking into.</p>
<img src="http://feeds.feedburner.com/~r/johnwright/~4/lHz06mBSke0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://johnwright.me/blog/tank-auth-groups-roles-admin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://johnwright.me/blog/tank-auth-groups-roles-admin/</feedburner:origLink></item>
		<item>
		<title>Xbox Kinect Is The New Killer Interface</title>
		<link>http://feedproxy.google.com/~r/johnwright/~3/bdnNY04MaAM/</link>
		<comments>http://johnwright.me/blog/xbox-kinect-is-the-new-killer-interface/#comments</comments>
		<pubDate>Thu, 16 Dec 2010 23:58:13 +0000</pubDate>
		<dc:creator>John Wright</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://johnwright.me/blog/?p=478</guid>
		<description><![CDATA[I&#8217;ve had a Kinect for a few weeks now and I&#8217;m very pleased with my purchase. The experience of controller-less navigation through the UI menus and playing of games is a very welcome addition to interacting with a computer. The Xbox 360 overall has turned out to be a great platform. With Xbox Live I [...]]]></description>
			<content:encoded><![CDATA[<p><!-- img src="http://johnwright.me/blog/wp-content/uploads/2010/12/Kinect-Adventures-Kickin-It.jpg" alt="" title="Kinect-Adventures-Kickin-It" width="350"  class="alignnone size-full wp-image-480" /--></p>
<p><img style="float: left; margin: 10px 20px 5px 0pt;" src="http://johnwright.me/blog/wp-content/uploads/2010/12/Kinect-Adventures-Kickin-It-150x150.jpg" alt="" title="Kinect-Adventures-Kickin-It" width="150" height="150" class="alignnone size-thumbnail wp-image-480" /></p>
<p>I&#8217;ve had a Kinect for a few weeks now and I&#8217;m very pleased with my purchase. The experience of controller-less navigation through the UI menus and playing of games is a very welcome addition to interacting with a computer. </p>
<p>The Xbox 360 overall has turned out to be a great platform. With Xbox Live I can download a bunch of game demos for free and buy full games downloaded and stored on the Xbox memory so I can play these games/demos without a disk. Also, the Xbox 360 <a href="http://www.engadget.com/2010/11/29/microsoft-in-talks-to-start-new-tv-service-using-the-360/" rel="external nofollow">is rumored to become a home online media hub</a> to compete with Google TV, Apple TV, Boxee etc. If this does happen, then the total of Kinect + Xbox Games + Xbox Online Media sounds like a sure winner to me, offering more than the competition. </p>
<p>Kinect is also in direct competition to the Nintendo Wii, and it has a lot of advantages over it. As an owner of both, I&#8217;ve been able to compare them from my own experience. While the Wii does offer some great family friendly fun with it&#8217;s simple standard games, I can see Xbox Kinect is going to take over where the Wii leaves off. <span id="more-478"></span></p>
<p>There are so many nice things about the controller-less experience of the Kinect. Using your full body in games is a fun and active workout as opposed to standard video gaming. You really need to try it out for yourself, it can really get you immersed in the game and it&#8217;s a lot of fun with two players. When you play with a group, it&#8217;s so nice to be able to just jump right in and start playing. I&#8217;ve had groups of ten or more over when we&#8217;ve played the Wii. It was a lot of fun, but the passing of the controller, putting on and removing the wrist strap and switching characters sometimes slowed things down. It wasn&#8217;t a big problem at all, but when compared to just walking in and out of game play, you notice how convenient the Kinect is. </p>
<p>I don&#8217;t always buy the newest gadgets when they come out (in fact my cell phone is still an iPhone 3G) but the Kinect is a product I recognized as truly a step forward in retail technology. It&#8217;s truly a step forward in human computer interaction that is available to everyone. I&#8217;ve been very pleased with my purchase. So far I&#8217;ve purchased the Kinect and games/accessories below (I also have an <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&#038;location=http%3A%2F%2Fwww.amazon.com%2Fs%3Fie%3DUTF8%26x%3D0%26ref_%3Dnb_sb_noss%26y%3D0%26field-keywords%3Depson%2520projector%26url%3Dsearch-alias%253Daps&#038;tag=wrightlabs-20&#038;linkCode=ur2&#038;camp=1789&#038;creative=390957" rel="external nofollow">epson projector</a><img src="https://www.assoc-amazon.com/e/ir?t=wrightlabs-20&#038;l=ur2&#038;o=1" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> which makes it about 10 ft. on my wall, it&#8217;s very nice). I&#8217;ve already had a lot of fun testing these out. I&#8217;m looking forward to having a larger group of friends over to try it out with everyone. Lots of fun and cool new technology!</p>
<div class="amazonLink"  >
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=wrilab-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=B003O6EE4U" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<div class="amazonLink"  >
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=wrilab-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=B002I0JBVY" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<div class="amazonLink"  >
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=wrilab-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=B002I0H9WM" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<div class="amazonLink" >
<iframe src="http://rcm.amazon.com/e/cm?lt1=_blank&#038;bc1=000000&#038;IS2=1&#038;bg1=FFFFFF&#038;fc1=000000&#038;lc1=0000FF&#038;t=wrilab-20&#038;o=1&#038;p=8&#038;l=as1&#038;m=amazon&#038;f=ifr&#038;md=10FE9736YVPPT7A0FBG2&#038;asins=B001EKTF60" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
</div>
<div style="clear:both" ></div>
<p>I see this as the beginning of much more than just new technology in gaming.</p>
<img src="http://feeds.feedburner.com/~r/johnwright/~4/bdnNY04MaAM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://johnwright.me/blog/xbox-kinect-is-the-new-killer-interface/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://johnwright.me/blog/xbox-kinect-is-the-new-killer-interface/</feedburner:origLink></item>
		<item>
		<title>Net Neutrality Pros And Cons: A Potentially Sticky Issue [OPINION]</title>
		<link>http://feedproxy.google.com/~r/johnwright/~3/lBeAfmPg-mI/</link>
		<comments>http://johnwright.me/blog/net-neutrality-pros-and-cons-a-potentially-sticky-issue-opinion/#comments</comments>
		<pubDate>Tue, 25 May 2010 18:45:55 +0000</pubDate>
		<dc:creator>John Wright</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[BitTorrent]]></category>
		<category><![CDATA[Comcast]]></category>
		<category><![CDATA[Computer law]]></category>
		<category><![CDATA[Federal Communications Commission]]></category>
		<category><![CDATA[Network neutrality]]></category>
		<category><![CDATA[Technology/Internet]]></category>

		<guid isPermaLink="false">http://iamjohnwright.com/blog/?p=204</guid>
		<description><![CDATA[Photo:eirikso [This article ponders just a small bit of the pros and cons concerning Net Neutrality. Understand that I'm absolutely pro Net Neutrality as a policy, but I'm a bit divided on whether or not government should impose regulations to protect Net Neutrality. In the end, it seems like government regulations may be absolutely necessary [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://farm4.static.flickr.com/3294/3055527027_65220276ee_m.jpg" alt="" /><br />
Photo:<a title="Internet is a series of tubes" rel="nofollow external" href="http://flickr.com/photos/47402349@N00/3055527027">eirikso</a></p>
<p>[This article ponders just a small bit of the pros and cons concerning Net Neutrality. Understand that I'm absolutely pro Net Neutrality as a policy, but I'm a bit divided on whether or not government should impose regulations to protect Net Neutrality. In the end, it seems like government regulations may be absolutely necessary to protect Net Neutrality, but whether the government will decide to regulate is another issue. Considering all this, Net Neutrality could be a potentially sticky issue for some time to come.]</p>
<p>My simple definition of Net Neutrality is&#8230;</p>
<p>&#8220;<em>ISPs don&#8217;t touch the bits and bytes flowing through their pipes</em>&#8221;</p>
<p>In other words, ISPs (Internet Service Providers) remain neutral concerning what internet traffic flows through their networks (or their &#8220;pipes&#8221;).  ISPs just provide dumb pipes for the traffic to flow through. They remain neutral by not actively doing anything to the traffic, like filtering it or blocking it in any way. This is the way the internet has operated since it&#8217;s beginning (for the most part).</p>
<div style="float: left;margin: 12px 15px 0 0"><a title="Comcast Tower" rel="nofollow external" href="http://flickr.com/photos/9682451@N02/3289914546"><img src="http://farm4.static.flickr.com/3343/3289914546_919746a66c_t.jpg" alt="" /></a></div>
<p>An example where an ISP acted in a non neutral way is when Comcast actively blocked BitTorrent traffic on its networks. In many ways, this is a fairly small thing they did. All paying customers of Comcast who did not use BitTorrent didn&#8217;t notice a thing. Comcast+BitTorrent users on the other hand, we&#8217;re stuck and would have been forced to switch ISPs in order to continue using BitTorrent, had the FCC not intervened.<br />
<span id="more-204"></span></p>
<p>This is small potatoes in comparison to what could be done by a non neutral ISP. Potentially, ISPs could exclude entire websites and networks of websites. They could offer preferred and speedier access to partnering digital media and news outlets. They could do a lot of potentially scary things. By controlling the network in such ways, ISPs could drastically change the internet as we know it to the extent that the network they offer could no longer truly be called &#8220;the internet&#8221;.</p>
<div style="float: left; margin: 12px 15px 0 0"><a title="Kevin Martin, Chairman Federal Communications Commission FCC during his keynote presentation at CTIA WIRELESS" rel="nofollow external" href="http://flickr.com/photos/31282765@N03/2949809284"><img src="http://farm4.static.flickr.com/3233/2949809284_88f6d5c206_s.jpg" alt="" /></a></div>
<p>On the other hand, the ISPs own the pipes and who is to stop them from being non neutral? In the Comcast case, the FCC tried to stop them but <a href="http://arstechnica.com/tech-policy/news/2010/04/court-throws-out-fccs-smackdown-of-comcast-p2p-blocking.ars" target="_blank" rel="external nofollow">in April 2010 it&#8217;s case was tossed by a federal court&#8230;</a></p>
<blockquote><p>The question before the court was whether the FCC had the legal authority to &#8220;regulate an Internet service provider&#8217;s network management practice.&#8221; According to a three-judge panel, &#8220;the Commission has failed to make that showing&#8221; and the FCC&#8217;s order against Comcast is tossed.</p></blockquote>
<p>In many ways this outcome is fair. If Comcast owns the pipes, who is to tell them what they can and can&#8217;t do with them? Or who should tell them what services they can and can&#8217;t offer? In a capitalist economy, shouldn&#8217;t the free market dictate that? Wouldn&#8217;t this cause competing ISPs to offer faster and better products? If their service is too restrictive, customers will go elsewhere right? Can&#8217;t  the free market and net neutrality co-exist without outside regulation?</p>
<p>In an ideal world the free market should provide a lot of natural regulation, but in reality, it doesn&#8217;t always provide enough. I&#8217;m reminded of Allen Greenspan&#8217;s words concerning the 2008 economic downturn&#8230;</p>
<blockquote><p>Mr. Greenspan said he made &#8220;a mistake&#8221; in his hands-off  regulatory philosophy, which many now blame in part for sparking the  global economic troubles. He quoted something he had written in March:  &#8220;Those of us who have looked to the self-interest of lending  institutions to protect shareholder&#8217;s equity (myself especially) are in a  state of shocked disbelief.&#8221; &#8211; <a href="http://online.wsj.com/article/SB122476545437862295.html" rel="external nofollow">WSJ</a></p></blockquote>
<p>He was shocked that lending institutions didn&#8217;t regulate themselves  in their own self-interest and the interest of their share holders. My point is that sometimes, regulation is necessary. Net Neutrality regulations may very well be necessary to keep the internet the same open place we know it as today.</p>
<p>But the internet and it&#8217;s openness has also created a lot of new issues. The impact of the internet on the world is huge and while it has solved many problems and made things possible that were previously impossible, it has also created new problems. For instance, ﻿﻿BitTorrent is commonly used to distribute pirated software, music, movies and more. Pirating software is a lot like stealing, except by making a copy of something, no physical good has been stolen. The main violation is that the creators of the pirated media don&#8217;t get any credit for the pirated copy. Don&#8217;t the creators have a right to some compensation or royalties for their work? Of course they do, but everybody knows you can pirate it on BitTorrent for free. Who is losing here? We all are to some degree, but most obviously it&#8217;s the creators of the work who are losing the most.</p>
<p>So maybe BitTorrent <em>should</em> be blocked. But of course there are other ways to share pirated copies on the internet. Potentially a non neutral ISP might have strong anti piracy policies active in their network. They might also be in partnership with music, media and software companies to provide a convenient solution to paying for digital products. Much like Apples iTunes and App Store. It seems if paying for digital products was easier than pirating them, most people would pay. I also believe that most people would want to pay and make sure the creators of media get paid for their work rather than pirate it.</p>
<p>In Conclusion (I need to wrap this up)<br />
The neutral internet is and has been a means for the little guy to actually compete with major players on this &#8220;world is flat&#8221; playing field. In turn this has sparked much innovation in technology and brought many benefits and progression. On the other hand some &#8220;<a href="http://en.wikipedia.org/wiki/Walled_garden_(technology)" rel="external nofollow">walled garden</a>&#8221; approaches to internet software have proved beneficial for the mainstream (Apple and Facebook?) , arguably helping mainstream users to reduce the information overload that is a side affect of the open and neutral internet. But &#8220;walled garden&#8221; control at a network level would drastically change the entire foundation of the internet. It&#8217;s really scary to think of an internet in any other form than the neutral one we&#8217;ve come to know.</p>
<img src="http://feeds.feedburner.com/~r/johnwright/~4/lBeAfmPg-mI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://johnwright.me/blog/net-neutrality-pros-and-cons-a-potentially-sticky-issue-opinion/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://johnwright.me/blog/net-neutrality-pros-and-cons-a-potentially-sticky-issue-opinion/</feedburner:origLink></item>
		<item>
		<title>Distributed Social Networks: The Digital Home [CONCEPT]</title>
		<link>http://feedproxy.google.com/~r/johnwright/~3/DRGjgPkKS_A/</link>
		<comments>http://johnwright.me/blog/distributed-social-networks-the-digital-home-concept/#comments</comments>
		<pubDate>Sat, 15 May 2010 00:07:00 +0000</pubDate>
		<dc:creator>John Wright</dc:creator>
				<category><![CDATA[Social Technology]]></category>

		<guid isPermaLink="false">http://iamjohnwright.com/blog/?p=257</guid>
		<description><![CDATA[Photo: Stuck in Customs A few days ago an analogy regarding social networks and their future crossed my mind. The analogy is &#8220;The Digital Home&#8221; as a distributed social network platform. The idea is that big social networks are kind of like massive communes where you have your own room (for instance my room on [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://iamjohnwright.com/blog/wp-content/uploads/2010/05/digital-homes-300x181.jpg" alt="" title="digital-homes" width="300" height="181" class="alignnone size-medium wp-image-273" /><br />
Photo: <a href="http://www.flickr.com/photos/stuckincustoms/382564223/" rel="external nofollow">Stuck in Customs</a></p>
<p>A few days ago an analogy regarding social networks and their future crossed my mind. The analogy is &#8220;The Digital Home&#8221; as a distributed social network platform. </p>
<p>The idea is that big social networks are kind of like massive communes where you have your own room (for instance my room on twitter is http://twitter.com/wrightlabs).<span id="more-257"></span> You operate on these social networks primarily on their terms. As we&#8217;ve seen with Facebook, changing privacy policies and settings on their site, they have the ultimate say in how the platform changes and what you can and can&#8217;t do. Leadership in how you &#8220;live&#8221; on Facebook (and other social networks) is centralized and decided by Facebook management. Another thing about big social networks is that, once you are on it, you are limited to that one social network. If you sign up for Facebook, you can&#8217;t interact with your friend who is on Twitter and vice versa. Unless you have an account with both networks and then hook up some kind of connection, but even then it&#8217;s too cumbersome and probably wouldn&#8217;t work that well. You really need to be part of the same network or &#8220;commune&#8221; to network and interact. </p>
<p>But the &#8220;digital home&#8221; idea decentralizes all this, instead of you being a part of a commune living within it&#8217;s walls, you own your own home (or site). This home would in most cases be your own domain name (I plan to make mine http://ijohnwright.com). Similar to how you don&#8217;t allow just anyone into your physical home, you would have privacy controls to keep strangers out of your &#8220;digital home&#8221; or allow people in to certain rooms. If you have family/friends that would like a room in your &#8220;digital home&#8221; you could give them one if you choose so it may not just be you living at your home (which is essentially your domain name). </p>
<p>The benefit is that you own it all, but with all this comes the responsibility of maintaining a home. Paying the hosting bill, if/when the site goes down, running in and fixing it etc.</p>
<p>If your home is on one domain name, then this &#8220;digital home&#8221; will need a way to communicate with other similar homes on the web to be a &#8220;distributed social network&#8221;. I mean if my friend on another site updates his &#8220;room&#8221; that I have access to with some pictures, it needs to show up in my stream. If I can only see whats going on and interact within my digital home, then it&#8217;s not a distributed social network. So we&#8217;ll need a distributed social network platform or open protocol that all these digital houses use to connect together. There are already many technologies that could be used to achieve what I&#8217;m describing. It would take packaging them together or reinventing them into an Open Social protocol (I&#8217;m not referring to Google&#8217;s).</p>
<p>Apparently I&#8217;m not the only one who has been thinking along these lines.</p>
<p>Since I first started thinking this, we&#8217;ve seen <a href="http://www.joindiaspora.com/" rel="external nofollow">Diaspora</a> launch in a <a href="http://www.kickstarter.com/projects/196017994/diaspora-the-personally-controlled-do-it-all-distr" rel="external nofollow">rather historic fashion</a>. The fact that some dudes about to graduate from NYU can kickstart their career in such a fashion is evidence that a lot of other people are embracing the distributed social network concept as well. </p>
<p>Also, I just heard Matt of WordPress giving a similar analogy while being interviewed by the Scoblizer at Big Omaha. I&#8217;m glad to hear Matt is thinking like this because when I originally came up with the idea, Matt&#8217;s WordPress software is what initially came to mind. </p>
<p>Doing a little bit of searching for this I found the <a href="http://diso-project.org/" rel="external nofollow">DiSo Project</a> which I&#8217;ve heard of years ago through <a href="http://factoryjoe.com/blog/" rel="external nofollow">Chris Messina</a>. Now I know exactly what DiSo is <img src='http://johnwright.me/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
<p>So I guess the distributed social network idea is nothing new but maybe now it will really start to gain some traction. It&#8217;s good to know many are working on this. I think the project that will win the most adoption is the one that makes it simple and straight forward concerning privacy and how to set it up. These are two big challenges but I&#8217;m sure someone will solve them.  </p>
<img src="http://feeds.feedburner.com/~r/johnwright/~4/DRGjgPkKS_A" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://johnwright.me/blog/distributed-social-networks-the-digital-home-concept/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://johnwright.me/blog/distributed-social-networks-the-digital-home-concept/</feedburner:origLink></item>
		<item>
		<title>SPARQL Query In Code: REST, PHP And JSON [TUTORIAL]</title>
		<link>http://feedproxy.google.com/~r/johnwright/~3/DU3_86BUSOc/</link>
		<comments>http://johnwright.me/blog/sparql-query-in-code-rest-php-and-json-tutorial/#comments</comments>
		<pubDate>Tue, 04 May 2010 16:23:07 +0000</pubDate>
		<dc:creator>John Wright</dc:creator>
				<category><![CDATA[Semantic Web]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rest]]></category>
		<category><![CDATA[sparql]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://iamjohnwright.com/blog/?p=37</guid>
		<description><![CDATA[Photo: sclopit SPARQL allows you to query a semantic web (i.e. RDF) data source. This post will cover some basics of SPARQL but it will mainly focus on how to run a SPARQL query in code, against a SPARQL endpoint live on the web. The code I&#8217;m using is PHP and JSON but the overall [...]]]></description>
			<content:encoded><![CDATA[<div style="float:left;margin: 10px 20px 5px 0" ><img src="http://johnwright.me/blog/wp-content/uploads/2010/05/sparql-tutorial-query-in-code.jpg" alt="Use SPARQL And Query On" title="sparql-tutorial-query-in-code" width="170" height="240" class="alignleft size-full wp-image-277" /><br />
Photo: <a rel="nofollow external" href="http://www.flickr.com/photos/stefanobe/4305452977/">sclopit</a>
</div>
<p>SPARQL allows you to query a semantic web (i.e. RDF) data source. This post will cover some basics of SPARQL but it will mainly focus on how to run a SPARQL query in code, against a SPARQL endpoint live on the web. The code I&#8217;m using is PHP and JSON but the overall steps are the same using any language.</p>
<p>(Here is the <a  href="http://johnwright.me/code-examples/sparql-query-in-code-rest-php-and-json-tutorial.php">tutorial demo</a> and the <a href="http://gist.github.com/380379" rel="external nofollow">full source code</a> for this tutorial.)</p>
<p>The overall steps are&#8230;</p>
<p>1. Constructing our SPARQL query<br />
2. Preparing our REST URL<br />
3. Make the HTTP request to the URL<br />
4. Parsing the response<br />
5. Use/Display the results</p>
<p><span id="more-37"></span></p>
<h3>1. Constructing our SPARQL query</h3>
<p>Our example SPARQL query will get the &#8220;abstract&#8221; section (i.e. brief description) of the RDF page for Honda Legend (<a href="http://dbpedia.org/page/Honda_Legend" rel="external nofollow">http://dbpedia.org/page/Honda_Legend</a>) in DBPedia</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">PREFIX dbp: <span style="color: #66cc66;">&lt;</span>http:<span style="color: #66cc66;">//</span>dbpedia<span style="color: #66cc66;">.</span>org<span style="color: #66cc66;">/</span>resource<span style="color: #66cc66;">/&gt;</span>
PREFIX dbp2: <span style="color: #66cc66;">&lt;</span>http:<span style="color: #66cc66;">//</span>dbpedia<span style="color: #66cc66;">.</span>org<span style="color: #66cc66;">/</span>ontology<span style="color: #66cc66;">/&gt;</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> ?abstract
<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#123;</span>
     dbp:Honda_Legend dbp2:abstract ?abstract <span style="color: #66cc66;">.</span> 
     <span style="color: #993333; font-weight: bold;">FILTER</span> langMatches<span style="color: #66cc66;">&#40;</span>lang<span style="color: #66cc66;">&#40;</span>?abstract<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'en'</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>(<a target="_blank" href="http://dbpedia.org/snorql/?query=PREFIX+dbp%3A+%3Chttp%3A%2F%2Fdbpedia.org%2Fresource%2F%3E%0D%0APREFIX+dbp2%3A+%3Chttp%3A%2F%2Fdbpedia.org%2Fontology%2F%3E%0D%0A+%0D%0ASELECT+%3Fabstract%0D%0AWHERE+{%0D%0A+++++dbp%3AHonda_Legend+dbp2%3Aabstract+%3Fabstract+.+%0D%0A+++++FILTER+langMatches%28lang%28%3Fabstract%29%2C+%27en%27%29%0D%0A}%0D%0A" rel="external nofollow">See this query run in DBPedia</a>)</p>
<p>In the SPARQL query, <strong><em>?abstract</em></strong> is a variable. The name is arbitrary (it could be called <em>?x</em>) and all variables in SPARQL start with the <strong><em>?</em></strong> (question mark). </p>
<p>The first line in the <strong><em>WHERE</em></strong> clause is a RDF triple. It&#8217;s a <strong>&#8220;subject predicate object&#8221;</strong> followed by a dot to end the triple (like the period of a sentence). </p>
<p>In this triple, the subject is <strong><em>dbp:Honda_Legend</em></strong> in it&#8217;s abbreviated form. We could also use it&#8217;s fully expanded form&#8230;<br />
<strong><em>&lt;http://dbpedia.org/resource/Honda_Legend&gt;</em></strong><br />
and we wouldn&#8217;t need to have the <strong><em>PREFIX dbp: &lt;http://dbpedia.org/resource/&gt;</em></strong> line in the query. </p>
<p>The predicate is <strong><em>dbp2:abstract</em></strong> in it&#8217;s abbreviated form. The predicate in a triple describes the relationship between the subject and the object.</p>
<p>The object in this triple is the variable <strong><em>?abstract</em></strong>. Or in other words, the object is what we are trying to find with this query. </p>
<p>To explain this query in English one could say &#8220;find the value of the <em>object</em> of the triple that matches the pattern &#8220;<strong><em>dbp:Honda_Legend dbp2:abstract object</em></strong>&#8221;</p>
<p>The second line in the <strong><em>WHERE</em></strong> clause is applying a filter to the results to only show the english version of the abstract. </p>
<p>Try these links for more info on the SPARQL query language. </p>
<p><a href="http://www.cambridgesemantics.com/2008/09/sparql-by-example/" rel="external nofollow">SPARQL by example &#8211; Cambridge Semantics</a> </p>
<p><a href="http://jena.sourceforge.net/ARQ/Tutorial/" rel="external nofollow">SPARQL Tutorial</a> </p>
<h3>2. Preparing our REST URL</h3>
<p>The URL we&#8217;ll send our request to will start with the SPARQL endpoint. For DBPedia it&#8217;s <strong><em>http://dbpedia.org/sparql</em></strong> and we&#8217;ll pass it some query string parameters, <strong><em>format</em></strong> and <strong><em>query</em></strong> (which is our SPARQL query).</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> getUrlDbpediaAbstract<span style="color: #009900;">&#40;</span><span style="color: #000088;">$term</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$format</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'json'</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> 
   <span style="color: #0000ff;">&quot;PREFIX dbp: &lt;http://dbpedia.org/resource/&gt;
   PREFIX dbp2: &lt;http://dbpedia.org/ontology/&gt;
&nbsp;
   SELECT ?abstract
   WHERE {
      dbp:&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$term</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; dbp2:abstract ?abstract . 
      FILTER langMatches(lang(?abstract), 'en')
   }&quot;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000088;">$searchUrl</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://dbpedia.org/sparql?'</span>
      <span style="color: #339933;">.</span><span style="color: #0000ff;">'query='</span><span style="color: #339933;">.</span><span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span>
      <span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;format='</span><span style="color: #339933;">.</span><span style="color: #000088;">$format</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">return</span> <span style="color: #000088;">$searchUrl</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I&#8217;ve taken it a step further by creating this function <strong><em>getUrlDbpediaAbstract</em></strong> which takes a parameter <strong><em>$term</em></strong> that lets us plug in any DBPedia (also Wikipedia) page name to get the abstract for. (<strong><em>Note:</em></strong> <strong><em>$term</em></strong> must be an actual page name from DBPedia/Wikipedia so that the URL <strong><em>http://en.wikipedia.org/wiki/</em>[page name]</strong> returns a non redirected Wikipedia article. The [page name] must be exact and it is case sensitive.)</p>
<p>The following approach to getting and using the data from this REST web service can be used for any REST web service that returns JSON as a response format. I&#8217;ve found this approach to be very simple and powerful. (I may write a separate post focusing on this approach).</p>
<h3>3. Make the HTTP request to the URL</h3>
<p>Making HTTP requests in PHP is pretty standard and easy task using <a href="http://php.net/manual/en/book.curl.php" rel="external nofollow">cURL</a> if you have it installed on Apache (which most web hosts do it seems). There is also a PEAR package <a href="http://pear.php.net/package/HTTP_Request2" rel="external nofollow">HTTP_Request2</a> which is another option but it requires PEAR to be installed.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> request<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// is curl installed?</span>
   <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'curl_init'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
      <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'CURL is not installed!'</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 curl handle</span>
   <span style="color: #000088;">$ch</span><span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</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;">// set request url</span>
   <span style="color: #990000;">curl_setopt</span><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: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">// return response, don't print/echo</span>
   <span style="color: #990000;">curl_setopt</span><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: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">/*
   Here you find more options for curl:
   http://www.php.net/curl_setopt
   */</span>		
&nbsp;
   <span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">return</span> <span style="color: #000088;">$response</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Above is a simple function <strong><em>request</em></strong> which will request our the <strong><em>$url</em></strong> and return the response body as a string. </p>
<h3>4. Parsing the response</h3>
<p>The request to our URL above will return a string of JSON data because that&#8217;s the format we specified. I chose JSON as the response format because there is a very easy way to parse a JSON string in PHP 5. We can simply use <strong><em><a href="http://php.net/manual/en/function.json-decode.php" rel="external nofollow">json_decode</a></em></strong> . <strong><em>json_decode</em></strong> has a bool parameter <strong><em>assoc</em></strong> which if set to <em>true</em> returns a PHP array. With this PHP array we can easily access all the data returned in our response. We&#8217;ll see how easy this is in the next section.</p>
<h3>5. Use/Display the results</h3>
<p>With our results in a PHP array, we can use a little function that I created to neatly print the contents of a PHP array called <strong><em>printArray</em></strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> printArray<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #339933;">,</span> <span style="color: #000088;">$spaces</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #000088;">$retValue</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>	
      <span style="color: #000088;">$spaces</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$spaces</span>
         <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000088;">$retValue</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$retValue</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_keys</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
         <span style="color: #000088;">$retValue</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$retValue</span><span style="color: #339933;">.</span><span style="color: #000088;">$spaces</span>
            <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;strong&gt;&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$key</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;/strong&gt;&quot;</span>
            <span style="color: #339933;">.</span>printArray<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> 
               <span style="color: #000088;">$spaces</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>		
      <span style="color: #000088;">$spaces</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$spaces</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #b1b100;">else</span> <span style="color: #000088;">$retValue</span> <span style="color: #339933;">=</span> 
      <span style="color: #000088;">$retValue</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; - &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$array</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&lt;br/&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">return</span> <span style="color: #000088;">$retValue</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This function can be used in development to see where the data you want is located in the array. </p>
<p>Putting our functions together now we can display and use the data returned.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> 
<span style="color: #000088;">$term</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Honda_Legend&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$requestURL</span> <span style="color: #339933;">=</span> getUrlDbpediaAbstract<span style="color: #009900;">&#40;</span><span style="color: #000088;">$term</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$responseArray</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span>
   request<span style="color: #009900;">&#40;</span><span style="color: #000088;">$requestURL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
   <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;h1&gt;DBPedia Abstract for 
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$term</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/h1&gt;
&nbsp;
&lt;h3&gt;Request URL:&lt;/h3&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$requestURL</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;br/&gt;
&nbsp;
&lt;h3&gt;Parsed Response: &lt;/h3&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> printArray<span style="color: #009900;">&#40;</span><span style="color: #000088;">$responseArray</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;br/&gt;
&nbsp;
&lt;h3&gt;Abstract: &lt;/h3&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$responseArray</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;results&quot;</span><span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;bindings&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;abstract&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>(<a  href="http://johnwright.me/code-examples/sparql-query-in-code-rest-php-and-json-tutorial.php">See this run live</a> or <a href="http://gist.github.com/380379" rel="external nofollow">check out the full example source code on GitHub</a>)</p>
<p>At the time of writing the results of<br />
<strong><em>echo printArray($responseArray);</em></strong><br />
look like this&#8230; </p>
<blockquote><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>head</strong><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>link</strong><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>vars</strong><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>0</strong> &#8211; abstract<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>results</strong><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>distinct</strong> &#8211; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>ordered</strong> &#8211; 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>bindings</strong><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>0</strong><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>abstract</strong><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>type</strong> &#8211; literal<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>xml:lang</strong> &#8211; en<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>value</strong> &#8211; The Honda Legend is a Mid-size luxury car made by the Japanese automaker Honda. It was originally developed as part of Project XX, a joint venture with the Austin Rover Group of Great Britain and was a twin of the Rover 800 series. The Legend was initially a four-door sedan, with a two-door coupé added later. It was the model which launched Honda&#8217;s upscale Acura brand in the United States. Honda was inspired by the word &#8220;legend&#8221; to create the first Honda vehicle with a V6. The first and second-generation Honda Legend was known as the Acura Legend in North American markets from 1986-1995, and in 1996 the third-generation was renamed as the Acura RL, while the Legend name is still used in Japan and other markets. Honda introduced the Legend as a flagship sedan to compete with the JDM Nissan Cedric and Nissan Gloria twins, the Toyota Crown, and later the Mazda Luce, and Mitsubishi Debonair. Unlike the Nissan twins and the Crown, the Legend is not used for taxi service. In the USA, the Legend competed with larger rear wheel drive V8 sedans Lexus LS and the Infiniti Q45, however, the Legend was marketed towards the slightly smaller Executive car vehicles that include the BMW 5 series, Audi A6, Mercedes-Benz E-Class, and the Jaguar S-type. The Legend hardtop coupe was introduced to compete with the Nissan Leopard coupe, the Toyota Soarer, and Mazda Cosmo.<br/><br/></p></blockquote>
<p>From this we can discover the path to get the <em>abstract</em> data we want, which is&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;h3&gt;Abstract: &lt;/h3&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$responseArray</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;results&quot;</span><span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;bindings&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;abstract&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>from the code block above (line 22).</p>
<h3>Conclusion</h3>
<p>In this tutorial we&#8217;ve seen how to construct a SPARQL query that gets the value of the <em>abstract</em> property of DBPedia page. We sent the query to a SPARQL endpoint (DBPedia) which is a REST web service URI. We have parsed the results by converting the JSON string response to a PHP array using <strong><em><a  href="http://php.net/manual/en/function.json-decode.php" rel="external nofollow">json_decode</a></em></strong>. This approach to consuming REST based web services is an easy way to access the data in the results. I may write a future post focusing on it.</p>
<p><a  href="http://johnwright.me/code-examples/sparql-query-in-code-rest-php-and-json-tutorial.php">A working demo of this tutorial is here</a> and <a href="http://gist.github.com/380379" rel="external nofollow">the full source code for the demo is on GitHub.</a> </p>
<p>If you have any questions or issues with this tutorial and/or demo source code, please leave them in the comments below.</p>
<img src="http://feeds.feedburner.com/~r/johnwright/~4/DU3_86BUSOc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://johnwright.me/blog/sparql-query-in-code-rest-php-and-json-tutorial/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		<feedburner:origLink>http://johnwright.me/blog/sparql-query-in-code-rest-php-and-json-tutorial/</feedburner:origLink></item>
	</channel>
</rss>

