Вопрос

$url = 'https://api.facebook.com/method/links.getStats?urls=http://google.com&format=json';
$get = json_decode($url, true);
echo 'Shares:' . $get['share_count'];

Why does this not return anything?

Это было полезно?

Решение

json_decode doesn't work with URLs, it expects string as parameter. You have to fetch response of this request and pass it to json_decode. Something like this:

$url = 'https://api.facebook.com/method/links.getStats?urls=http://google.com&format=json';
$get = json_decode(file_get_contents($url), true);
echo 'Shares:' . $get['share_count'];

Другие советы

You can get the total count by using the following code.

$url = $this->get_json_values( 'https://api.facebook.com/method/links.getStats?urls=http://google.com&format=json' );
$json = json_decode( $url, true );
$facebook_count = isset( $json[0]['share_count'] ) ? intval( $json[0]['share_count'] ) : 0;

$facebook_count will give the total share count for facebook.

Thanks

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top