PHPism Blog

Converting everything into PHP code.

Fix Facebook login All Version

Dear customers;

We have recently received many issues related to Facebook login. After research we found that sometimes Facebook denies access using file_get_contents() php function.
To resolve this issue in your website, please follow these 2 steps.
1- In /inc/functions/additional.php
Find:
?>

Add before it:
function file_get_contents_curl($url) {
$ch = curl_init();

curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
if(isset($_SERVER['HTTP_USER_AGENT']))
{
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
}
else
{
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0');
}
$data = curl_exec($ch);
curl_close($ch);

return $data;
}

2- In /inc/login.php
Find:
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$graph_url = "https://graph.facebook.com/me?fields=id,name,email&access_token="
. $params['access_token'];
$user = json_decode(file_get_contents($graph_url));

Replace with:
$params = json_decode(file_get_contents_curl($token_url));
$graph_url = "https://graph.facebook.com/me?fields=id,name,email&access_token=".$params->access_token;
$user = json_decode(file_get_contents_curl($graph_url));

3- In /admin/settings_social_autopost.php
Find:
$token_url = "https://graph.facebook.com/oauth/access_token?"."client_id=".$appId."&redirect_uri=".urlencode($page_protocol.$config['adminurl'].'/settings_social_autopost.php')."&client_secret=".$appSecret."&code=".$code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$ACCESS_TOKEN = $params['access_token'];
$graph_url = "https://graph.facebook.com/me?access_token=".$params['access_token'];
$user = json_decode(file_get_contents($graph_url));

Replace with:
$token_url = "https://graph.facebook.com/oauth/access_token?"."client_id=".$appId."&redirect_uri=".urlencode($page_protocol.$config['adminurl'].'/settings_social_autopost.php')."&client_secret=".$appSecret."&code=".$code;
$params = json_decode(file_get_contents_curl($token_url));
$ACCESS_TOKEN = $params->access_token;
$graph_url = "https://graph.facebook.com/me?access_token=".$ACCESS_TOKEN;
$user = json_decode(file_get_contents_curl($graph_url));

That’s it.
Hope that we helped.

Best Regards
PHPism Team

Leave a Reply