1/28/2012

[php で OAuth を使ってみる:第八回] Facebook のチェックインスポット取得する

Facebook のチェックインスポットを OAuth を使って取得してみる。
※この情報は 2012 年 1 月現在のものです。仕様が変更される可能性は充分にあるので、ご了承ください。

OAuth を使った認証の流れは Facebook に文字を post する を参照。その際に、認証時に使う「scope」を「publish_checkins」にする。

チェックインスポットを検索して表示する
<?php
$query = $_POST['query'];
// php の文字コードが utf-8 であっても文字化けしたので記載
header('Content-type: application/json; charset=utf-8');

require_once 'facebook/facebook.php';

$facebook = new Facebook(array(
  'appId' => 'xxxxxxxxxxxxxxxx', // 以前のエントリーで確認した「App ID/API Key」
  'secret' => 'xxxxxxxxxxxxxxxx' // 以前のエントリーで確認した「アプリの秘訣」
));
$attachment = array(
  'type' => 'place',
  'center'=> '34.985214,135.758593', // 任意の緯度経度
  'distance' => '1000', // 1,000m 四方が対象
  'access_token' => 'xxxxxxxxxxxxxxxx' // 以前のエントリーで取得した access_token
);
$query = 'モスバーガー'; // 検索語句
// スポット名で検索する場合
if ($query) $attachment['q'] = $query;

$place = $facebook->api('/search', 'get', $attachment);
// 結果は json で返ってこないので、json_encode してやる
echo json_encode($place);
?>
例えばこの php をアプリ本体の Javascript で読み込み、結果で出た json を Javascript で解析してレイアウト後に表示する、などができる。

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.