Created
April 30, 2019 02:59
-
-
Save yhira/1cfcf02a8bafcb4f3238931c47c73f97 to your computer and use it in GitHub Desktop.
Facebookのリアクションカウントを取得する関数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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