Sunday, May 15, 2011

file_get_contents php method call to facebook loading very slow

when try to get content from an URL, php provide a method "file_get_contents", sometime when you use this method it might takes too long to load.

the following code is used to get the user basic profile from facebook,

$graph_url = "https://graph.facebook.com/me?" . $access_token;
$fbdetail = file_get_contents($graph_url);
$user = json_decode($fbdetail);

for me, file_get_contents takes 5-10 second to load the above code, some say its an DNS issues, below is another function to replace file_get_contenst, it takes less than 3 second for me to load.

just include the below code and replace "file_get_contents" with "get_url"

function get_url($url)
 {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  $tmp = curl_exec($ch);
  curl_close($ch);
  return $tmp;
 }

Tuesday, May 3, 2011

Login with Facebook Button using Javascript

The following shows how to put a "login with facebook" button on your website, visitor can skip the register process and you can request a access token with this script and save it in a cookie or session, also you can access the visitor facebook basic information.

First, assume that your page is http://www.mylovelystory.com/index.html
Then, put this script inside your head tag

//Replace YOUR_APP_ID to you facebook application ID <script src="http://connect.facebook.net/en_US/all.js#appId=YOUR_APP_ID&xfbml=1"></script> //Replace YOUR_APP_ID to you facebook application ID

And inside you HTML tag, DivLogin consist the login button, DivLogged consist the profile picture of the logged facebook users

Login with Facebook

hope this acticle helps, drop a comment and leave your email if you need my help.