何度やってもgetSession()がない、と怒られる。
facebook.php覗いても確かにない。
どうやら2011年の5月に使用変更があったようです。
https://developers.facebook.com/blog/post/503/
旧ログイン
$facebook = new Facebook(…);
$session = $facebook->getSession();
if ($session) {
// proceed knowing you have a valid user session
} else {
// proceed knowing you require user login and/or authentication
}
新ログイン
$facebook = new Facebook(…);
$user = $facebook->getUser();
if ($user) {
// proceed knowing you have a logged in user who's authenticated
} else {
// proceed knowing you require user login and/or authentication
}
これでやっても 500 Internal Server Errorがでる。
SSLのエラーログを見ると、(/Applications/MAMP/logs/ssl_error.log)
[Tue May 01 16:02:50 2012] [error] [client 127.0.0.1] File does not exist: /Applications/MAMP/htdocs_ssl/phpsdk/myapp
myappじゃなくてmytestだ。
まだつながらない。
次にPHPのエラーログを見てみる。(/Applications/MAMP/logs/php_error.log)
[01-May-2012 16:12:25] PHP Parse error: syntax error, unexpected T_CONSTANT_EN\ CAPSED_STRING, expecting ')' in /Applications/MAMP/htdocs_ssl/phpsdk/mytest/ind\ ex.php on line 6
ENCAPSED_STRINGってなんだ。
http://d.hatena.ne.jp/sho-yamasaki/20110210/1297325586
全角のスペースが入ってるだと。。。調べたけど入ってない。
多分arrayの書き方が間違ってるんだろうな。
動かなかったコード
<?php require('facebook.php'); $data = array( 'appId' => 'foobar', 'secret' => 'hoge' 'cookie' => 'true', ); $facebook = new Facebook($data); $user = $facebook->getUser(); if ($user) { // proceed knowing you have a logged in user who's authenticated } else { // proceed knowing you require user login and/or authentication } ?>
テキストエディタを替えて全部コピーしたら動いた。
なんだったんだろう。
https://developers.facebook.com/blog/post/503/
<?php require 'facebook.php'; // Create our application instance // (replace this with your appId and secret). $facebook = new Facebook(array( 'appId' => 'YOUR_APP_ID', 'secret' => 'YOUR_APP_SECRET', )); // Get User ID $user = $facebook->getUser(); // We may or may not have this data based // on whether the user is logged in. // If we have a $user id here, it means we know // the user is logged into // Facebook, but we don’t know if the access token is valid. An access // token is invalid if the user logged out of Facebook. if ($user) { try { // Proceed knowing you have a logged in user who's authenticated. $user_profile = $facebook->api('/me'); } catch (FacebookApiException $e) { error_log($e); $user = null; } } // Login or logout url will be needed depending on current user state. if ($user) { $logoutUrl = $facebook->getLogoutUrl(); } else { $loginUrl = $facebook->getLoginUrl(); } // This call will always work since we are fetching public data. $naitik = $facebook->api('/naitik'); ?> <!doctype html> <html xmlns:fb="http://www.facebook.com/2008/fbml"> <head> <title>php-sdk</title> <style> body { font-family: 'Lucida Grande', Verdana, Arial, sans-serif; } h1 a { text-decoration: none; color: #3b5998; } h1 a:hover { text-decoration: underline; } </style> </head> <body> <h1>php-sdk</h1> <?php if ($user): ?> <a href="<?php echo $logoutUrl; ?>">Logout</a> <?php else: ?> <div> Login using OAuth 2.0 handled by the PHP SDK: <a href="<?php echo $loginUrl; ?>">Login with Facebook</a> </div> <?php endif ?> <h3>PHP Session</h3> <pre><?php print_r($_SESSION); ?></pre> <?php if ($user): ?> <h3>You</h3> <img src="https://graph.facebook.com/<?php echo $user; ?>/picture"> <h3>Your User Object (/me)</h3> <pre><?php print_r($user_profile); ?></pre> <?php else: ?> <strong><em>You are not Connected.</em></strong> <?php endif ?> <h3>Public profile of Naitik</h3> <img src="https://graph.facebook.com/naitik/picture"> <?php echo $naitik['name']; ?> </body> </html>