<?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>Software Production Management &#187; facebook</title>
	<atom:link href="http://www.simpleprojectz.com/tag/facebook/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.simpleprojectz.com</link>
	<description>project management &#38; software development &#38; personal stuff</description>
	<lastBuildDate>Wed, 05 Oct 2011 01:46:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Facebook &amp; CodeIgniter &#8211; Definitive Guide</title>
		<link>http://www.simpleprojectz.com/2008/10/facebook-codeigniter/</link>
		<comments>http://www.simpleprojectz.com/2008/10/facebook-codeigniter/#comments</comments>
		<pubDate>Wed, 22 Oct 2008 03:29:29 +0000</pubDate>
		<dc:creator>Alberto Dominguez, PMP</dc:creator>
				<category><![CDATA[SW Development]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[facebook]]></category>

		<guid isPermaLink="false">http://www.simpleprojectz.com/?p=185</guid>
		<description><![CDATA[I spent more hours than the expected starting a simple development for a new Facebook application that runs on top of CodeIgniter. First I read A LOT, then I checked for solutions and, of course, I read a little bit more. After several hours reading and coding I knew it wasn&#8217;t good at all so [...]]]></description>
			<content:encoded><![CDATA[<p>I spent more hours than the expected starting a simple development for a new Facebook application that runs on top of CodeIgniter. First I read A LOT, then I checked for solutions and, of course, I read a little bit more. After several hours reading and coding I knew it wasn&#8217;t good at all so I decided to start a new integration method from the scratch using only the code I found as guide. Here&#8217;s the result:</p>
<p><strong>0. Environment</strong></p>
<p>Before you get excited just review a few things before spent time reading another useless post <img src='http://www.simpleprojectz.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<ul>
<li>PHP Version 5.2.6</li>
<li>MySQL 5 (It doesn&#8217;t affect Facebook integration at all but it is good to know)</li>
<li>HostMonster is my hosting provider (it is not a marketing initiative, just they have a nice environment set up for PHP)</li>
<li>Facebook platform 5 &#8211; I upload a copy of it to ensure you get the same copy I used.</li>
<li>CodeIgniter 1.6.3 &#8211; latest version available at the time of this post.</li>
</ul>
<p><strong>1. Do not hack CodeIgniter</strong></p>
<p>I found a lot of solutions about hacking CodeIgniter by overriding functions or classes. NO, YOU DO NOT HAVE TO DO IT.</p>
<p><strong>2. Install facebook library as plugin</strong></p>
<ul>
<li>Download the facebook platform ZIP</li>
<li>Unzip the files wherever you want -outside your Code Igniter application</li>
<li>Copy the WHOLE content under <em>/php</em> folder (under means files inside the folder and not with the container folder) to yout <em>./system/plugins</em> folder inside Code Igniter application -<strong>Note:</strong> Yes, you have to include the <em>jsonwrapper</em> folder.</li>
<li>Rename the <em>facebook.php</em> file to <em>facebook_pi.php</em></li>
</ul>
<p>It is done! You already have installed facebook framework as plugin.</p>
<p><strong>3. Autoload facebook plugin </strong>[optional]</p>
<p>To avoid the need of including the &#8220;load plugin&#8221; sentence inside every controller/class you can modify the <em>./system/config/autoload.php</em> to include facebook plugin as required. It is almost a requirement for Facebook applications. Add to your <em>$autoload['plugin']</em> array the facebook plugin. It could look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$autoload</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'plugin'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'facebook'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Note: </strong>If you skip this step you will have to include the following line every time you need facebook functionality available.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>load<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>plugin<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'facebook'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>4. Setup your application </strong>[updated]</p>
<p>Update your configuration file and set the .<em>/system/application/config/config.php</em> and change it as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'uri_protocol'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;PATH_INFO&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$config</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'base_url'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;APPLICATION_URL&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>Note:</strong> If you want to test your application locally, you can set <em>base_url</em> to <em>http://127.0.0.1/path_to_your_app/</em></p>
<p><strong>5. Extend your Controller class</strong></p>
<p>Because I&#8217;m building a 100% facebook application, all my controllers require facebook API available. So I decided to extend my main Controller class as follows &#8211; BTW: By the date of this post the CodeIgniter documentation <strong>WAS WRONG</strong> about how to extend the core classes (<a href="http://codeigniter.com/user_guide/general/core_classes.html" target="_blank">CodeIgniter User Guide Version 1.6.3 &#8211; Creating Core System Classes</a>). It is not true -i.e. it is false- that you have to extend the <em>CI_ClassName</em>. At least it didn&#8217;t work for me at all. Extend the <em>ClassName</em> directly.</p>
<p>Facebook Controller class should be placed under <em>./system/application/libraries</em> folder.</p>
<p><strong>Filename:</strong> XX_Controller.php (where &#8220;XX_&#8221; is the prefix you set on your<em> ./system/application/config/config.php</em> file &#8211; subclass_prefix parameter)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> FB_Controller <span style="color: #000000; font-weight: bold;">extends</span> Controller <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Facebook application key</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$API_KEY</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'YOUR_API_KEY'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$facebook</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$uid</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">/*
	 * Custom Controller constructor.
	 * Adds Facebook support.
	 *
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> FB_Controller<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		parent<span style="color: #339933;">::</span><span style="color: #004000;">Controller</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;">// Authentication key</span>
		<span style="color: #000088;">$secret</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'YOUR_SECRET_KEY'</span><span style="color: #339933;">;</span> 
&nbsp;
		<span style="color: #666666; font-style: italic;">// Prevent the 'Undefined index: facebook_config' notice from being thrown.</span>
		<span style="color: #000088;">$GLOBALS</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'facebook_config'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'debug'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">NULL</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Create a Facebook client API object.</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>facebook <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Facebook<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>API_KEY<span style="color: #339933;">,</span> <span style="color: #000088;">$secret</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>uid <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>facebook<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>require_login<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Note:</strong> Inside the facebook application I built I always require an authenticated users, so that&#8217;s why I do have the <em>require_login() </em>call. However you can also validate if the user already have the application added. You should do this manually inside controller classes because there are a few exceptions where it is needed that user is authenticated but without adding the application.</p>
<p><strong>6. Create your own controllers</strong></p>
<p>Now you can extend your controllers from your Custom Controller. <strong>Note:</strong> You do not have to add any include or require sentence in your other classes, CodeIgniter will load directly your <em>XX_Controller.php</em> file -if it doesn&#8217;t load please check the <em>config.php</em> file and validate the <em>subclass_prefix</em> parameter.</p>
<p>Below you will find an example of a Controller that will validate if the user has the application added in his/her profile.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Welcome <span style="color: #000000; font-weight: bold;">extends</span> XX_Controller <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> Welcome<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		parent<span style="color: #339933;">::</span><span style="color: #004000;">XX_Controller</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;">// Check if the application has been added by the user</span>
		try <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>facebook<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>api_client<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>users_isAppAdded<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>facebook<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>redirect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>facebook<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>get_add_url<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: #b1b100;">return</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$x</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// Clear cookies for your application and redirect them to a login prompt</span>
			<span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>facebook<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>expire_session<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$facebook</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>redirect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>facebook<span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>get_login_url<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>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// You should place your Controller's methods below.</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>7. Get out and celebrate</strong></p>
<p>CodeIgniter is running as cleaner as you expected and Facebook API was included nicely!</p>
<p>I hope you find this post useful as I said at the beginning.</p>
Note: There is a file embedded within this post, please visit this post to download the file.
]]></content:encoded>
			<wfw:commentRss>http://www.simpleprojectz.com/2008/10/facebook-codeigniter/feed/</wfw:commentRss>
		<slash:comments>49</slash:comments>
		</item>
	</channel>
</rss>

