Jeremy
Jeremy

Reputation: 105

Facebook API : Can't get number of Facebook likes on an online server

I'm trying to get the number of likes of a Facebook account with this PHP line :

$content = file_get_contents('http://graph.facebook.com/facebook?fields=likes');

This work on my local server. The problem is this doesn't seems to work on an online server (var_dump($content) return the value "false").

I don't really quite understand why this works only in my local server. Did someone already encounter this problem?

Thanks

Upvotes: 0

Views: 45

Answers (1)

Jonathan
Jonathan

Reputation: 2877

Consider switching to curl, it's much better for interacting with APIs.

$postData = http_build_query(
    [
        'fields' => 'likes'
    ]
);

$ch = curl_init();

$endpoint = sprintf('%s?%s', 'http://graph.facebook.com/facebook', $postData);

curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);

$response = curl_exec($ch);

Upvotes: 1

Related Questions