Skip to content

Instantly share code, notes, and snippets.

@yhira
Created April 30, 2019 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yhira/1cfcf02a8bafcb4f3238931c47c73f97 to your computer and use it in GitHub Desktop.
Save yhira/1cfcf02a8bafcb4f3238931c47c73f97 to your computer and use it in GitHub Desktop.
Facebookのリアクションカウントを取得する関数
//Facebookのリアクションカウントを取得する
function fetch_facebook_count($url){
//アクセストークンの入力
$access_token = '取得したアクセストークンを入力';
//URLをURLエンコード
$encoded_url = rawurlencode( $url );
//Facebookにリクエストを送る
$request_url = 'https://graph.facebook.com/?id='.$encoded_url.'&fields=engagement&access_token='.$access_token;
$response = wp_remote_get( $request_url );
$res = 0;
//取得に成功した場合
if (!is_wp_error( $response ) && $response["response"]["code"] === 200) {
$body = $response['body'];
//ジェイソンオブジェクトに変換する
$json = json_decode( $body );
//リアクションカウントをシェア数として取得する
$res = (isset($json->{'engagement'}->{'reaction_count'}) ? $json->{'engagement'}->{'reaction_count'} : 0);
}
return intval($res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment