Sunday, March 13, 2011

Merging Facebook SDK with CodeIgniter 2.0 v1.1

After talking with folks at codeigniter.com and a bit of peer review I have come to the conclusion that the library doesn't need to have variables declared every time. In fact you can create a file named facebook.php in the /application/config folder and throw in:
$config['appId'] = 'your_app_id';
$config['secret'] = 'your_app_secret';
$config['cookie'] = true;
After which, go to /application/config/autoload.php and add 'facebook' to the libraries array and it will automatically load those parameters into your library.
Afterwards your calling of the functions should look like this in the __construct() function:
function __construct()
{
parent::__construct();

$this->fb_session = $this->facebook->getSession();
$this->fb_me = null;
// Session based API call.
if ($this->fb_session) {
try {
$this->fb_uid = $this->facebook->getUser();
$this->fb_me = $this->facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
$this->fb_appid = $this->facebook->getAppId();

$this->data['appid'] = $this->fb_appid;
$this->data['me'] = $this->fb_me;
$this->data['uid'] = $this->fb_uid;
}
Hope this helps you with what you were looking for! Stay tuned for more as I discover more interesting things about the facebook sdk and codeigniter. Next time I will include a tutorial on how to make sure that your users get authenticated correctly via the system.

Saturday, March 12, 2011

Merging Facebook SDK with CodeIgniter 2.0

Merging the Facebook PHP SDK with CodeIgniter turned out to be not so simple of a task, due to the fact that no one at all has tried documenting how to do it in SDK 2.1.x and with CI 2.0, so because none have tried I am now here to show you all how, instead of being greedy.

What you need is the facebook php sdk which you can download at GitHub (https://github.com/facebook/php-sdk/) and CodeIgniter 2.0 (http://codeigniter.com).

First off you need to have set up an application, which you have probably done via multiple sites prior to me, so for now I will assume you have an app registered with facebook. Just make sure you use the iframe system, because FBML is being deprecated and will eventually be phased out.

Secondly, you need to uncompressed and upload CI2.0 to your server, and setup all the configurations the way you need them. I use Request_URI for mine.

After that, uncompress the facebook-php sdk and look in the folder /src and grab the facebook.php in there, and move it to the /application/libraries folder of your CI installation, after which rename it to Facebook.php to ensure compatibility with CI lib class.

Now came the fun part.

Unfortunately unlike a lot of other tutorials say you can just create a config file for facebook, well that was for the modified and butchered versions of the facebook api. I am going to show you how to pull up facebook api unaltered and with complete support of both. Took me over an hour or so tweaking it to work.

In the controller you want to load the facebook api, find the __constructor() and place:
function __construct()
{
parent::__construct();

$params['appId'] = 'your_app_id';
$params['secret'] = 'your_app_secret';
$params['cookie'] = true;
$this->load->library('facebook', $params);

}
This will allow you to use facebook as $this->facebook->api() and other functions. Such as what I use in the __construct() right after the load->library event:
$this->fb_session = $this->facebook->getSession();
$this->fb_me = null;
// Session based API call.
if ($this->fb_session) {
try {
$this->fb_uid = $this->facebook->getUser();
$this->fb_me = $this->facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
$this->fb_appid = $this->facebook->getAppId();
I personally use $this->fb_* in order to make sure that I don't accidentally use the same name as a variable previously declared in the system.

Hope this helps you guys, looking for a solution like I was. :)

Wednesday, November 18, 2009

phpDesigner 7 Released

I am not sure how long they have had it out but its been out for a bit and I can tell you its much better than the version 6, it even includes support for JavaScript frameworks like jQuery! Simply awesome, go check it out and try it out!

Friday, June 26, 2009

A PHP Sorceror Comes Forth

Good Day Folks,
I decided to create my own blog on the subject of PHP, mainly due to the fact that I have done a few others but nothing on this subject. I am the owner of http://ultimate-battle-online.com and I am the main coder for http://battlestar-wars.com so I guess you can say I have enough experience to give back to the players and people who wish to learn just how I began my journey as a PHP Sorceror. Well folks, I started off as many of you all who are viewing this did, asking myself, "How the heck do you even start something like that?!" Lol... I know the feeling all too well, and with eager studying and constant practice I got to where I am now.

I will say however that PHP Programming is not as complicated as you think though! There will be walls that you have to either climb, knock down, or dig under to get past. When you do you, essentially you will be a better coder and designer for it.

Many people have asked me over and over to create tutorials because the way I explain things hits the spot, and a few will say I explain it in too complex a terms. In responce, I will give you resources that will allow you to brush up on terms and theories while I work on the tutorials I will be submitting.

The first secret to my success is a priceless code editor. The one and only I use is phpDesigner. This editor essentially is a buffed up notepad, which quite a few of you folks have been using to start off. Trust me I have been there. This editor not only shows line numbers but also gives tips for your code as to what functions complete your typing. But the moral of the situation is, no matter what this is a well worth editor to get your hands on! Totally worth the money. Only downside for cross OS support would be that it is a Windows-only software so I will look into some software for Mac & Linux.

The second secret of my success is a good portable server which you can whether you have internet access or not. I have tried many different server compilations and I have tosay that XAMPP does the best in all regards. They constantly update their revisions to match the latest of the Apache, PHP, MySQL, Perl configs and make sure that when you download the next version you have the best or most secure of the releases. XAMPP also has different packages for all major Operating Systems as well. I use the Windows version myself, due to the fact that I run most of my software on Windows 7 with phpDesigner.

The third secret of my success would be an awesome browser, one which has support for all major Operating Systems. My secret? Mozilla Firefox, the god of all browsers. It is free, and also most OSs will have a compiled version for you, for example Ubuntu Linux has an update less than a week after new versions of Firefox come out.

Well my time is almost up so heres a list of tutorials that will help you get started:
http://www.mpsoftware.dk/tutorials.php
Good luck!!

deth4uall - The PHP Sorceror