0

I'm tring do get the total number of sharing a link on facebook with the function "file_get_contents".

This is my code:

$url = x;
$data = file_get_contents('http://graph.facebook.com/?id=".$url');
$obj = json_decode($data, true);
$like_no = intval($obj->{'shares'});
echo $like_no;

I tried this and it doesn't work. But file_get_contents is enabled on my server so i don't find the solution... If you can help me! Thanks in advance :)

polYc
  • 21
  • 3
  • $data is what i get from the facebook graph. For exemple: http://graph.facebook.com/?id=http://facebook.com – polYc Feb 11 '15 at 22:52

2 Answers2

2

Thanks with your help i find the solution and without the api (i'm happy :))

For people intereset in:

$url = x;
$data = file_get_contents('http://api.facebook.com/restserver.php?method=links.getStats&format=json&urls='.$url);
$obj = json_decode($data, true);
$like_no = $obj[0]['total_count'];
polYc
  • 21
  • 3
0

You need an access token to access that link, which can be obtained by registering as a developer and creating an application.

Some links that might be helpful :

1) Facebook developers : https://developers.facebook.com/

2) PHP SDK : https://developers.facebook.com/docs/reference/php/4.0.0

3) Graph API documentation : https://developers.facebook.com/docs/graph-api

You need to connect to facebook graph api using an OAuth2 client and it requires you to read their documentation.

L.E. It seems that I am wrong. You don't actually need an access token to get those informations.

Check this answer : Get the number of facebook *shares* of a specific URL

Community
  • 1
  • 1
bandrei2408
  • 110
  • 7
  • Thanks i was thinking i don't need it. Have you a little hack for fast authentification (i have app and secret key). I would like to not use facebook php sdk because i think it gonna slow my website. – polYc Feb 11 '15 at 22:58
  • First of all, the SDK won't slow your website. Secondly, the easiest way is to use their PHP SDK, otherwise you will have to manually code the OAuth2 client library. – bandrei2408 Feb 11 '15 at 23:01
  • So if i have to use their SDK, i need to integrate there files to my website. They say that "The Facebook SDK for PHP v4 requires PHP 5.4 or greater.". I have a wordpress website which use previous version of PHP. How can i do? – polYc Feb 11 '15 at 23:07
  • Have you tried https://developers.facebook.com/docs/reference/php/3.2.3 PHP SDK v3.2.3 ? – bandrei2408 Feb 11 '15 at 23:09
  • I'm tring with it but they don't speak about how get link information like number of share. I've got this for the connection: `require 'facebook-php-sdk/src/facebook.php'; $facebook = new Facebook(array( 'appId' => 'xxx', 'secret' => 'xxx', )); ` I don't know what to do next... – polYc Feb 11 '15 at 23:26