Skip to content

Instantly share code, notes, and snippets.

@mgng
Created March 19, 2012 01:26
Show Gist options
  • Save mgng/2089189 to your computer and use it in GitHub Desktop.
Save mgng/2089189 to your computer and use it in GitHub Desktop.
facebook API を使ってニュースフィードを取得する
<?php
$app_id = 'あなたのapp ID';
$app_secret = 'あなたのapp secret key';
$my_url = 'あなたのapp url';
$code = isset($_GET['code']) ? $_GET['code'] : '';
if ( $code === '' ) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id={$app_id}&scope=read_stream&redirect_uri=".urlencode($my_url);
echo "<a href=\"{$dialog_url}\">認証+アプリ連携開始</a>";
} else {
$token_url = "https://graph.facebook.com/oauth/access_token?client_id={$app_id}&client_secret={$app_secret}&code={$code}&redirect_uri=".urlencode($my_url);
$access_token = file_get_contents( $token_url );
if ( $access_token === false ) {
echo '認証失敗';
} else {
$graph_url = "https://graph.facebook.com/me/home?{$access_token}";
$data = json_decode( file_get_contents( $graph_url ) );
// $data にニュースフィードデータが入ってくる
echo '<pre>' . print_r( $data, true ) . '</pre>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment