PHP SDK & Graph API base Facebook Connect Tutorial

Hello Friends

In My current application I’ve used facebook Graph API to fetched data from facebook with the help of  PHP SDK. I’ve explored so many tutorials on web but I found some links are very helpful and easy to understand Here I’m sharing those links. By surfing through these links you can get to know these below things

1 . How you can create your application in Facebook and How you can use APP ID and APP Secret? 

2. How you can connect to Facebook from your Application?

3. How you can fetch data with the help of Facebook Graph API and PHP-SDK?

4. How you can Design your Facebook Application?

So here are some useful links.

  1. http://www.londatiga.net/it/how-to-create-facebook-application-with-php-for-beginner/
  2. http://thinkdiff.net/facebook/php-sdk-3-0-graph-api-base-facebook-connect-tutorial/
  3. http://net.tutsplus.com/tutorials/php/wrangling-with-the-facebook-graph-api/
  4. http://www.joeyrivera.com/2010/facebook-graph-api-app-easy-w-php-sdk/
  5. http://net.tutsplus.com/tutorials/javascript-ajax/design-and-code-an-integrated-facebook-app/

To Create a New Application in Facebook.

https://developers.facebook.com/apps

Graph API

https://developers.facebook.com/docs/reference/api/

PHP SDK refrence

https://developers.facebook.com/docs/reference/php/

FB Tool for Graph API Explorer

http://developers.facebook.com/tools/explorer

Hope it helps.


profile for Viral Solani at Stack Overflow, Q&A for professional and enthusiast programmers

Advertisements

How to Install Apache, PHP, MySQL and PHPMyAdmin in Ubuntu 11.04

If you are a PHP based web developer, you need all the software running and configured properly. Here I am talking about installing them One by One in your Ubuntu Desktop. We are installing all the applications from terminal.

How to Open Terminal:
So, to fire up the terminal follow any of these steps:

  1. If you are running Unity Desktop, click on the Ubuntu Logo at top left corner and type Terminal in the search application bar. Then click on the terminal icon.
  2. If you are running GNome Desktop, click on Applications->Accessories->Terminal
  3. For shortcut, you can also press Ctrl+Alt+T at once, to open the terminal.

How to install Apache:

1. Make sure you have the internet connection. To install apache execute the following command in the terminal:

1
sudo apt-get install apache2

It takes some time to download and install apache. After the setup completes, type http://localhost/ in your browser window to make sure apache is installed and running properly. If you see the page with It Works!, the setup of apache2 completes successfully.

How to Install PHP:

1. To install PHP 5, type following commands in the terminal one by one:

1
2
sudo apt-get install php5
sudo apt-get install libapache2-mod-php5

The first line installs PHP5 in the computer. The second one provides the PHP5 module for the Apache 2 webserver. If second one is not installed, then Apache cannot parse PHP codes in a web page.

2. After installing PHP5 and PHP module for apache, restart the apache with following code:

1
sudo /etc/init.d/apache2 restart

3. While restarting the apache server, if you see a warning as “Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName”, then you can fix this by creating a file with the Server name. To do this type the following command in the terminal:

1
sudo gedit /etc/apache2/conf.d/fqdn

When the text editor opens, type “ServerName localhost” inside the file and click Save. Then close it. Now restart again with the above code and you will see that the warning message has gone.

4. Now, we have successfully installed php and apache web server. However, still we don’t know if PHP is successfully installed. To check this, create a file inside /var/www/ folder named test.php as:

1
sudo gedit /var/www/test.php

and write following code in it

1
<?php   phpinfo();  ?>

Save the file and type this in browser: http://localhost/test.php

If you see the various information about PHP and installed modules there, then we can confirm that Apache is parsing PHP codes. Hence the installation is successful up to this point.

How to Install MySQL:

1. To install MySQL Server in ubuntu, type following code in terminal window:

1
sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

This will install latest mysql server and other necessary PHP modules for recognizing mysql functions from PHP code. While installing MySQL server, you may require to enter the password for MySQL root user.

How to Install PHPMyAdmin:

1. To Install PHPMyAdmin, type the following codes in the terminal:

1
sudo apt-get install phpmyadmin

While installing PHPMyAdmin, you may require to select the web server. In such case, tick the Apache2 Server and proceed with the installation. You may also require to input MySQL root user password during installation.

Once the installation completes, type this in your browser window to confirm the successful installation of PHPMyAdmin: http://localhost/phpmyadmin/index.php.

Now, you are finished. Your environment is setup and you can enjoy using all these applications. Next, you can install other applications that may be necessary such as Eclipse, GIMP etc.

The Right Way to Get a File Extension in PHP

I made a recent search on retrieving file extensions in PHP.

I found out that a lot have been re-inventing the wheel. They have been creating code for functionality that PHP already has. This is one example of re-inventing the wheel

function get_file_extension($file_name)
{
return substr(strrchr($file_name,’.’),1);
}

Another example was this:

function file_extension($filename)
{
return end(explode(“.”, $filename));
}

PHP already has a function that does the same thing and more.

Welcome, pathinfo.

$file_path = pathinfo(‘/www/htdocs/your_image.jpg’);
echo “$file_path [‘dirname’]\n”;
echo “$file_path [‘basename’]\n”;
echo “$file_path [‘extension’]\n”;
echo “$file_path [‘filename’]\n”; // only in PHP 5.2+


// Output
/www/htdocs
your_image.jpg
jpg
your_image

A much easier way to use the constants:
[PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME]

PATHINFO_DIRNAME – the directory
PATHINFO_BASENAME – the file name
PATHINFO_EXTENSION – the extension
PATHINFO_FILENAME – the filename without the extension

echo pathinfo(‘/www/htdocs/your_image.jpg’, PATHINFO_EXTENSION);

// Output

jpg

Zend Framework Form(Zend_Form) tutorial

Here you can see how you can use Zend_Form component for creating html form easily, handling filters and errors messages and how to see how easily you can fetch data from the data source and present that in html form and let the visitors change/modify the existing data and save changes to the data source.
I am gona break this topic in three steps.
1. Creating Zend_Form
Although I’ve already discussed creation of Zend_Form in some of my articles but it will be better to discuss it here again.
Creating Zend form is simple is this.

class CustomForm extend Zend_Form
{
public function init()
{
}
}

defined a class by extending it from Zend_Form. This allow access to many of methods already defined in Zend_Form.
The only thing you need to do is to override init() method and create your own element such as input box, select statement, checkboxes, radio buttons and so on.
Defining your own elements don’t take much code. Have a look at the following code.

class CustomForm extend Zend_Form
{
public function init()
{
$this->setMethod('post');
$this->setAction('process/form');

$username = $this->createElement('text','username');
$username->setLabel('Username:')
->setAttrib('size',50);

$this->addElement($username);
}
}

In the code above we have overridden init() method of Zend_Form, set the form request method and action and then defined input element called username. You can put as many elements in this form as you wish. The last statement is used to add element to the forms. To add several elements to the form at once, use addElements() passing array of elements as an argument. You will look an example in the next example.

My entire form look likes

class CustomForm extends Zend_Form
{
public function init()
{
$this->setMethod('post');
$id = $this->createElement('hidden','id');
$firstname = $this->createElement('text','firstname');
$firstname->setLabel('First Name:')
->setAttrib('size',50);
$lastname = $this->createElement('text','lastname');
$lastname->setLabel('Last Name:')
->setAttrib('size',50);
$username = $this->createElement('text','username');
$username->setLabel('Username:')
->setAttrib('size',50);
$email = $this->createElement('text','email');
$email->setLabel('Email:')
->setAttrib('size',50);
$password = $this->createElement('password','password');
$password->setLabel('Password:')
->setAttrib('size',50);
$password2 = $this->createElement('password','password2');
$password2->setLabel('Confirm Password::')
->setAttrib('size',50);
$register = $this->createElement('submit','register');
$register->setLabel("Register")
->setIgnore(true);
$this->addElements(array(
$firstname,
$lastname,
$username,
$email,
$password,
$password2,
$id,
$register
));
}
}

Most of the functions are simple and self explanatory. The only method that I think I’d better explain here is setIgnore() method. The method setIgnore() has a very valuable usage. Well, if we see the entire form you will feel that we need all values when form is submitted. The only value we don’t need is the value of the submit button as this only used to submit the form. We are not interested in its value, so we call setIgnore() method passing Boolean value true.

2. As we have now created a form, next step is to create its object in controller and write code that will save data for us. Create a controller as

class UsersController extends Zend_Controller_Action
{
.....

public function addAction()
{
//$this->_helper->layout->disableLayout();
$users = new Users();
$form = new CustomForm();
$this->view->form = $form;

if ($this->getRequest()->isPost()) {
$formData = $this->_request->getPost();
if ($form->isValid($formData)) {
if ($formData['password'] != $formData['password2']) {
$this->view->errorMsg = "Password and Confirm Password must match.";
$this->render('add');
return;
}
unset($formData['password2']);
unset($formData['register']);
$users->insert($formData);
}
}
}
}

What we are doing here, is pretty simple. Create an object of Users model. Well I haven’t discussed Users model yet. I would discuss it for you soon. The next line creates an object of the form created earlier. Next we assign the form to the view template, in our case the template is add.phtml.
Next few lines are of much importance.
We check whether the request is first one or the post back. If form has been submitted, method $this->getRequest()->isPost() return ture. In this case we will need to take care of the data being submitted.
To handle the post back we get the data been posted back through form, check its validity, check if the password and confirm password matches, and insert the values in the “users” table using $users->insert(); statement.
If form is not valid, the code for inserting the data in the database will not be executed.
Now lets discuss “Users” model.
We have a table in our database called “users” containing different fields such as username, firstname, lastname etc.
We will need to create our model as

class Users extends Zend_Db_Table
{
protected $_name = "users";
}

That’s it, a simple model.
3. The third step in creating a full fledge form application in Zend Framework is presentation layer, called view/template.
In your scripts/users/ directory create add.phtml and put the following code in it

<h3>Add User</h3>
<?php
if ($this->errorMsg) {
echo $this->errorMsg;
}
?>
<?php
// for displaying form
echo $this->form;
?>

The code above is self explanatory.
The above process only shows a form and submits data to the database. If you want to create edit form, you will need to make a bit of change in your controller.
Let’s create another action called editAction containing the following code.

class UsersController extends Zend_Controller_Action
{
.....

public function editAction()
{
//$this->_helper->layout->disableLayout();
$users = new Users();
$form = new CustomForm();

$id = $this->_getParam("id",1);
$select = $users->select()
->where("id = ?",$id);
$data = $users->fetchRow($select);
$form->populate($data->toArray());
if ($this->getRequest()->isPost()) {
$formData = $this->_request->getPost();
if ($form->isValid($formData)) {
if ($formData['password'] != $formData['password2']) {
$this->view->errorMsg = "Password and Confirm Password must match.";
$this->render('add');
return;
}
unset($formData['password2']);
unset($formData['register']);
//Zend_Debug::dump($formData);exit;
$users->update($formData,"id = $id");
}
}
$this->view->form = $form;
}

}

You can clearly see that I’ve made only few lines of changes.
First I get the id of the user through $this->_getParam() method. I then create a select statement on the users table and then fetch a row. Once I get the data row, I populate the form using populate() method.
The only thing you will need now, is to create a view template in your scripts/users/ directory named edit.phtml and write the following code.

<?php
if ($this->errorMsg) {
echo $this->errorMsg;
}
?>
<?php
// for displaying form
echo $this->form;
?>

Well I was thinking to explain everything in three steps, but I think it would be incomplete if I don’t explain how to show the data inserted in the database. For this reason I going to add 4th step.
4. Showing list of data
Create an action called indexAction in your controller with the following code.

class UsersController extends Zend_Controller_Action
{
public function indexAction()
{
//$this->_helper->layout->disableLayout();
$users = new Users();
$data = $users->fetchAll($users->select());
$this->view->data = $data->toArray();
//Zend_Debug::dump($data->toArray());
}

.....

}

First we create an object of our mode. And then fetch the data using the fetchAll() method. The last statement assigns the fetched data to the view template.
Now create scripts/users/index.phtml and write the following code in it.

<h4>List of users</h4>
<h5><a href="add">Add new user</a></h5>
<table>
<thead> 
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
<th>Email</th>
<th>Action</th>
</tr>
</thead>
<tboody>
<?php foreach ($this->data as $d) {?>
<tr>
<td><?=$d['firstname']?></td>
<td><?=$d['lastname']?></td>
<td><?=$d['username']?></td>
<td><?=$d['email']?></td>
<td><a href="edit/id/<?=$d['id']?>">Edit</a></td>
</tr>
<?php }?>
</tbody>
</table>

Here we are creating a table that shows the list of data.

Automatically refresh the web page every 5 seconds, with PHP

Redict the web page with PHP

you can easily redirect to the different web page using simple php script.By using PHP header() function you can easily redirect to new page without having new extra clicks.

the following PHP script redirect the user to bugphp.com

1 <?php
2 header( 'Location: http://www.bugphp.com/' ) ;
3 ?>

make sure,it will not work if you sent any data to the browser before this function.if you write echo or print or any HTML statements or functions before the redirection you will get an error,to avoid this use PHP output buffering as follows

1 <?php
2 ob_start();
3 echo 'header test';
4 header( 'Location: http://www.bugphp.com/' ) ;
5 ob_flush();
6 ?>

Automatically refresh the web page every 5 seconds, with PHP

1 <?php
2 $url=$_SERVER['REQUEST_URI'];
3 header("Refresh: 5; URL=\"" . $url . "\""); // redirect in 5 seconds
4 ?>

how to make Alphabet Navigation Menu with PHP?

In this post we are going to discuses about alphabet navigation.before that do you know generating alphabets in a loop.if don’t know look at below example once

1 <?php
2
3 for ($i=65; $i<=90; $i++) {
4 echo chr($i);// chr returns specific character
5 //out put :ABCDEFGHIJKLMNOPQRSTUVWXYZ
6 }
7 ?>

In the above example chr() function returns specific character & chr() also accepts negative numbers as an ascii code[char(-100)]

You can make above output with range() function,look the example :

1 <?php foreach(range('A','Z') as $i) {
2 echo $i; }
3 ?>

output:ABCDEFGHIJKLMNOPQRSTUVWXYZ

now you can easily understand below alphabet navigation menu

1 <?php
2 $LetterList = range('A','Z');
3 foreach ($LetterList as $litter) {
4 echo '<a href="http://example.com/letter.php?letter=' . $litter. '">' .$litter. '</a>'.' ';
5 }
6 ?>

Outpup : A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

you can make above output in another way

1 <?php
2 $LetterList = array_merge(array('0-9'),range('A','Z'));
3 foreach ($LetterList as $value) {
4 echo '<a href="http://example.com/letter.php?letter=' . $value . '">' .$value . '</a>'.' ';
5 }
6 ?>

out put:A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

The description of the functions used in this tutorial are as follows

chr() this function return a spesific character

range() this function crate an array containing range of elements

foreach()foreach is used to loop over all elements of an array

array_merge() this function merges one or more arrays