Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not connect to Facebook with a curl request

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.
like image 674
retri Avatar asked Apr 10 '12 11:04

retri


People also ask

How to invalidate Facebook access token?

To invalidate a Page or User access token, the person that created the token will need to remove and then re-add the App. This will invalidate all access tokens created by that person for the App.

What type of API does Facebook use?

The Graph API is the primary way to get data into and out of the Facebook platform. It's an HTTP-based API that apps can use to programmatically query data, post new stories, manage ads, upload photos, and perform a wide variety of other tasks.

What is API on Facebook?

What is the Facebook API? The Facebook Graph API is an HTTP-based API that allows developers to extract data and functionality from the Facebook platform. Applications can use this API to programmatically query data, post in pages and groups, and manage ads, among other tasks.


1 Answers

Apparently this is related with the round-robin DNS structure of Facebook. I guess one of the addresses of the Facebook failed and the server was still trying to connect to that address because of the DNS cache.

The hosting provider had to clear their DNS caches to resolve the problem.

For more information about the round-robin DNS, you may visit http://en.wikipedia.org/wiki/Round-robin_DNS.

like image 153
retri Avatar answered Oct 27 '22 18:10

retri