0

I am trying to start using APIs, doing calls and so forth. Just barely starting to learn. Found a way to get Facebook shares on a post using the graph api.

I did this with PHP; here is the code:

$response = file_get_contents('https://graph.facebook.com/? id=mydomain.com');

echo $response;

this is the response that I get:

{"id":"http://sportsmockery.com/2014/11/hey-bears-fire-everyone/","shares":22} 

What I want is to somehow get the share count (22) into a variable so I can do stuff with it…(i changed the domain that gets that share count to my domain.com);

anyway; I am not sure what is the standard way to do this, if you control what is received with how you do your call; or if you just get the full response and pull out what you want…

Been looking around and have not been able to find anything that will really help with this.

I am hoping someone can help me with this…

All the best, G

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
gman_donster
  • 331
  • 3
  • 12

2 Answers2

3

Use json_decode() which will make all of the properties easily accessible.

$parsedResponse = json_decode($response);
$count = $parsedResponse->shares;

echo $count;
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
RichardBernards
  • 3,146
  • 1
  • 22
  • 30
1

This is a json response.

Parsing JSON file with PHP http://php.net/manual/en/function.json-decode.php

Community
  • 1
  • 1
gurel_kaynak
  • 546
  • 6
  • 11