0

I am trying to get the number of likes of a particular page on my website and store it in an array. Stuck in middle, i figured out that i can get the number of likes along with other data by the following code:

/*$site="http://graph.facebook.com/?ids=http%3a%2f%2xxxxxxxx.com/abc.php";
$graph= file_get_contents($site);

the output is as follows:

{"http:\/\/xxxxxxxx.com\/abc.php":{"id":"http:\/\/xxxxxxxx.com\/abc.php","shares":75,"comments":3}}

is there a way i can just store the number of likes i.e. in this case 75 in a php array?

I tried explode(); but the problem is that the url that i would be using wont be of a constant length.

4
  • 1
    That data format is called JSON, so look up json_decode in the manual. Btw., if you would use the PHP SDK, it would do the necessary stuff for you automatically. Commented Feb 15, 2013 at 12:15
  • @CBroe yea i guessed it was JSON but was just curious if this can be done using PHP by the same output results. Thanks anyways Commented Feb 15, 2013 at 12:21
  • Why don't you use the PHP SDK? Its a lot easier and its more future-proof. Commented Feb 15, 2013 at 13:14
  • @FacebookAnswers, what if i have multiple like buttons on the same page and if they arent the like buttons for facebook pages but my website's particular pages Commented Feb 15, 2013 at 19:05

1 Answer 1

1

This is a json string decode it, use json_decode you will get the output as array. ref : http://php.net/manual/en/function.json-decode.php

$array  = json_deocde($json_string, true);

echo "<pre>";
print_r($array);
Sign up to request clarification or add additional context in comments.

3 Comments

Did get the values in array but a stupid question, it says 'Array (....[shares]=>75.....)' How do i get this to a variable? What index should i give to the array?
$var = $array['shares'];
I tried that , it didn't work. It gave a following Notice: Undefined index: shares in C:\xampp\htdocs\graphapi.php on line 19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.