Joshc
Joshc

Reputation: 3853

Trying get var object from facebook graph but can seem to echo the variable I need

I am getting facebook graph information for a URL. So I can echo how many shares this URL has had on facebook.

This is how I am getting the graph object...

$facebook = json_decode(file_get_contents( "https://graph.facebook.com/?ids=".get_permalink()), TRUE);

and if I print this...

print_r( $facebook );

it outputs this array...

Array
(
    [http://mysite.co.uk/wp/2013/08/hello-world/] => Array
        (
            [id] => http://mysite.co.uk/wp/2013/08/hello-world/
            [shares] => 3
        )

)

OK so technically if I am successfully getting this array then I can output my share count like so...

echo $facebook['shares'];

But it is not working! Nothing is being echo'ed.

Can any please enlighten me to where I am going wrong?


Thanks in advance
Josh

Upvotes: 0

Views: 18

Answers (1)

Carsten Massmann
Carsten Massmann

Reputation: 28196

try the following:

echo $facebook['http://mysite.co.uk/wp/2013/08/hello-world/']['shares'];

Your reply is an array in another array, the first key is 'http://mysite.co.uk/wp/2013/08/hello-world/'.

You could also do

$a=array_values($facebook);
echo $a[0]['shares'];

or

list($a)=array_values($facebook);
echo $a['shares'];

Upvotes: 2

Related Questions