04 May 2013

Search Facebook Users from your website using Graph API

Today through this post of mine, I will describe you How to search a user on Facebook using it's graph api from your website

Here we will use simple graph api provided by facebook, For more info refer this url Graph API

We need to pass three parameters to url "https://graph.facebook.com" through which we can search any type of content from a 3rd party website

For eg if you want to search for wall post having content say "watermelon" then I will call url of the following form

https://graph.facebook.com/search?q=watermelon&type=post

Please note that user don't need to search pass access token to search for post data,but if you want to search for specific user then you have to pass access token

Below is the simple script that you can use to implement this feature on your website



<?php
 require 'src/facebook.php';

 $facebook = new Facebook(array(
  'appId'  => '465199043533380',
  'secret' => '611cc4009b15a1d6b00fe5775a1c0d5b',
 ));

 if(!empty($_POST)){
 
  $user = $facebook->getUser();
  
  if ($user) {
   $logout_url = $facebook->getLogoutUrl();
   $response_params = array();
   parse_str($logout_url, $response_params);
   $token = $response_params['access_token'];
   $email = $_REQUEST['email'];
   $contents = file_get_contents("https://graph.facebook.com/?q=$email&type=user&access_token=$token");
  } else {
   $params = array("response_type" => "token");
   $loginUrl = $facebook->getLoginUrl();
   header("location:$loginUrl");
  }
 
 }

?>
 
<?php
 require 'src/facebook.php';

 $facebook = new Facebook(array(
  'appId'  => '465199043533380',
  'secret' => '611cc4009b15a1d6b00fe5775a1c0d5b',
 ));

 if(!empty($_POST)){
 
  $user = $facebook->getUser();
  
  if ($user) {
   $logout_url = $facebook->getLogoutUrl();
   $response_params = array();
   parse_str($logout_url, $response_params);
   $token = $response_params['access_token'];
   $email = $_REQUEST['email'];
   $contents = file_get_contents("https://graph.facebook.com/?q=$email&type=user&access_token=$token");
  } else {
   $params = array("response_type" => "token");
   $loginUrl = $facebook->getLoginUrl();
   header("location:$loginUrl");
  }
 }
?>
 <!doctype html>
 <html xmlns:fb="http://www.facebook.com/2008/fbml">
  <head>
   <title>Facebook Graph Search</title>
   <style>
    body {
     font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
    }
    h1 a {
     text-decoration: none;
     color: #3b5998;
    }
    h1 a:hover {
     text-decoration: underline;
    }
   </style>
  </head>
  <body>
   <table cellpadding = "0">
    <tr>
     <td>
      <h1>Facebook Graph Search</h1>
     </td>
    </tr>
    <tr>
     <td>
     <form method = "post">
      <input type = "text" name  = "email">
      <input type = "submit">
     </form>
    </tr>
    <tr>
     <td>
      <?php 
       if(!empty($contents)){
        print_r($contents);
       }
      ?>
     </td>
    </tr>
     
   </table>
  </body>
 </html>

Download Complete Files from Here

No comments:

Post a Comment