Thursday, April 21, 2011

Posting to Facebook's friend wall - PHP


Hi,

This post is again a continuation of my previous posts related to Facebook Graph API. This one will sum up all the earlier post with a new functionality of posting to your own or friends wall. In this also I will be using the $facebook->api() to post in wall. Click here to download the entire source code.

My current page looks like:

In the above I have a form for updating my Facebook status. When I click the image of my friends a popup comes up and lets me post to that fiends wall as follows:



Code and explanation:

Tuesday, April 19, 2011

Get Facebook friend list - PHP


Hi,

This is the continuation of my previous post, were I explained how to create and work with Facebook session in your website. First you need to download the facebook.php class file. This is basically the back bone of Graph API. Click here to download the entire source code of this post. In this post I will be demonstrating how to get a list of all the friends in Facebook, along with their profile thumbnail, username, first/last name and profile ID.

Now Facebook provides an excellent API, known as FQL. This stands for Facebook Query Language. In FQL there are few predefined set of tables. All you have to do is to run simple SQL queries on those tables and fetch data. But since these are provided by Facebook, so we will not be using our conventional mysql_connect() etc to connect to the database. This is where the Graph API comes into play.

For running a FQL query this is what you need to write:


$fql    =   "YOUR_FQL_QUERY";
$param  =   array(
     'method'    => 'fql.query',
     'query'     => $fql,
     'callback'  => ''
);
$result_set   =   $facebook->api($param);


The $facebook->api() is the Graph API. $param array is defining the task that need to be performed, in this case it is a FQL query. Before getting into the explanation of this post, lets see how the page looks:


In the above I have listed all the friends thumbnails in <ul>, <li> form. When you click on any of the thumbnails. you are taken to its profile page.

Code & Explanation:

Sunday, April 17, 2011

Facebook login - FConnect Graph API (PHP)


Hi,

In this post I will be discussing about Graph API. The most basic use of it is when you want the users to register in your website via their Facebook profile. The advantage of doing that is, it saves all the email authentication, captcha etc validations. It simply fetches the users existing details from what is there in Facebook for that user. The use of Graph API is imense, which I hope to cover in my future post. In this I will be just concentrating on letting the users register in a website using the Graph API.

First and the most important thing, is to create your own App in Facebook. This is a very straight forward process, You just need to enter the App name, and URL. An App can be considered as the gateway between your website and Facebook's data. Follow this link to create a new App. Once you have set up your website you will be given a App ID and secret key. This will be needed in your code. So preserve this information carefully.

Now coming to the code. First click here to download the code. The main aspect of this code is the facebook.php file. This is the PHP SDK for Facebook Graph.

Explanation:

First you need to initialise the Facebook object by the App ID and secret key which you got in the above steps by:

$facebook = new Facebook(array('appId'  => 'YOUR_API_KEY','secret' => 'YOUR_SECRET_KEY','cookie' => true,));

Thursday, April 14, 2011

Parse CSV and enter in database


Hi,

CSV is one of the most convenient ways of importing and exporting data. CSV is nothing but a simple text files with fields seperated by ",". It can be edited by any spreadsheet editor, such that each cell is seperated by a ",". So if you want to parse a CSV file, you can do it by simply reading the file by fopen() of PHP, read each line, explode each line with a deliminator as ",".

But there is a simpler way to do that by using the PHP function, fgetcsv(). This reads 1 row at a time and returns the cell's in that row as an array. But before we start writing the code, the format of the CSV needs to be predetermined. That means the sequence of fields should be decided before starting the parsing.

Download the source code with the database. In my example I am maintaining the following table schema:

CREATE TABLE IF NOT EXISTS `csv_table` (
  `id` int(10) NOT NULL DEFAULT '0',
  `username` varchar(30) DEFAULT NULL,
  `phone` varchar(10) DEFAULT NULL,
  `email` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`id`)
)


Before going into the code and explanation lets see the output of the code. This is the content of my CSV file:


Id,Username,Phone,Email
10021,John,22190097,john@mailinator.com
10022,Paul,556447885,paul@mailinator.com

The output of the above after executing my code is as:



In the above I am converting the CSV into table structure and storing in the database. 

Code and Explanation:

First I am reading the CSV file and traversing each line with the following code:

<?php
if (($handle = fopen("sample.csv", "r")) !== FALSE) {
$i=0;
while (($data = fgetcsv($handle, 0, ",")) !== FALSE)   // 0 specifies unlimited size of each row seperated by ","
{
                // code to manipulate with the CSV data
  }

}
?>

Now the code to create the table and updating the database is as:

Generate RSS Feed with PHP

Hi,

If you are looking for code to generate RSS via PHP, then I guess you already know what RSS is. So I am coming straight to the source code for generating it. In my example below, I am generating a RSS feed for the listing details of vaiours movies, which would contain, the movie name, image, description and a link to the details page of the particular movie.

I am keeping a database table for storing the details of the various movies. The table schema is as follows:

CREATE TABLE IF NOT EXISTS `rss` (
  `id` int(10) NOT NULL auto_increment,
  `movie_name` varchar(100) collate latin1_general_ci default NULL,
  `category` varchar(50) collate latin1_general_ci default NULL,
  `pic` varchar(50) collate latin1_general_ci default NULL,
  `link` varchar(100) collate latin1_general_ci default NULL,
  `description` varchar(300) collate latin1_general_ci default NULL,
  PRIMARY KEY  (`id`)
)

As you see in the above schema, I have categorized the various movies. This will make it more dynamic. Now before going into the actual code and its explanation, lets see how the RSS feed looks like:



Code & Explanation:

First I have created a simple .htaccess file, for URL rewriting. My idea was, user will have the category_name.xml as the URL and the list will come according the category name. Here is the htaccess:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.xml$ example.php?q=$1 [QSA,L]

In the above, all the url's ending with XML will be sent to example.php and the xml file name as the query string i.e. "?q="

Now coming to the code. The file example.php does all the hard work with the following:

First the database connection is build and the SQL query as:

<?php
mysql_connect("localhost","root");
mysql_select_db("test");