I have created a simple Facebook application some weeks ago. I was getting user information through the Facebook API by asking permission from the users. But it stopped working last week out of nothing and I have been looking for an answer since then.
Basically I used to use file_get_contents to get the user information from Facebook API. But I tried changing it to cURL after it started failing, but still not working. I tried using the Facebook PHP-SDK, even debugged the code to makeRequest method, but I get the same return in all methods.
When a cURL request is made to Facebook Open Graph URL, the request fails either with error number 7 or 28. They are both error codes for unable to connect to the site or getting time out.
My site is on a shared hosting, I have tried using cURL to get other sites, and It works fine. But when I try to get even http://www.facebook.com, cURL returns FALSE.
The hosting has SSL certificate, has cURL enabled etc. And it was working fine some time ago. I have read through various threads and posts about this, tried many different cURL options, and none of them seem to work.
Any ideas?
index.php
$auth_url = "https://www.facebook.com/dialog/oauth?client_id=" . $app_id . "&redirect_uri=" . urlencode($GLOBALS['canvas_page']) . "&scope=publish_stream,email";
$signed_request = $_POST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (empty($data["user_id"])) {
echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {
$_SESSION['oauth_token'] = $data['oauth_token'];
$_SESSION['user_id'] = $data["user_id"];
$user = new User($_SESSION['user_id'], $_SESSION['oauth_token']);
User class
function __construct($fid, $at) {
$this->facebook = new Facebook(array(
'appId' => "<APP_ID>",
'secret' => "<FACEBOOK_SECRET_ID>",
));
$this->fid = $fid;
$this->token = $at;
if ($data = $this->getGraph()) {
...
}
}
public function getGraph() {
$session = $this->facebook->getUser();
if ($session) {
try {
$access_token = $this->facebook->getAccessToken();
$attachment = array('access_token' => $access_token);
//$result = $this->facebook->api('/' . $this->fid, 'GET', $attachment);
$result = $this->facebook->api('/' . $this->fid, 'GET');
return $result;
} catch (FacebookApiException $e) {
return false;
}
}
}
Notes:
- After checking the Facebook PHP SDK methods, I've realized that I do not need to send access token, as it will set it if access token is not in the $params.
- It goes down to makeRequest() method in sdk, and returns false from the curl_exec.