Created
August 22, 2012 00:21
-
-
Save tyubo/3420764 to your computer and use it in GitHub Desktop.
Facebook 投稿ページのサンプル
This file contains hidden or 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
| <?php | |
| // PHP SDK の読み込み | |
| require_once './src/facebook.php'; | |
| // キャンパスページのURL | |
| $baseUrl = '???'; | |
| // プロトコルの判別 | |
| $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' | |
| ? 'https://' | |
| : 'http://'; | |
| // ログイン後にリダイレクトするページのURL | |
| $afterLoginUrl = $protocol.$baseUrl; | |
| // アプリのインスタンス生成(初期化) | |
| $facebook = | |
| new Facebook( | |
| array('appId' => '???', | |
| 'secret' => '???')); | |
| // ユーザーIDを取得 | |
| $user = $facebook -> getUser(); | |
| if($user){ | |
| // ユーザIDが取得できたら | |
| // 必要な情報を取得 | |
| try{ | |
| // 自分のIDを取得 | |
| $me = $facebook -> api('/me'); | |
| } catch (FacebookApiException $e) { | |
| echo '<pre>'.htmlspecialchars(print_r($e,true)).'</pre>'; | |
| $user=null; | |
| }}else{ | |
| // ユーザIDが取得できていない(ログインしていない)なら | |
| // ログイン画面のURLを取得 | |
| $url = $facebook -> getLoginUrl( | |
| array('redirect_uri'=>$afterLoginUrl, | |
| // ユーザに変わって投稿する権限 | |
| 'scope'=>'publish_stream')); | |
| // ログイン画面にリダイレクト | |
| echo "<script>top.location.href='$url';</script>"; | |
| } | |
| // ウォールに投稿 | |
| if(isset($_POST['message'])){ | |
| $res = null; | |
| $mes = htmlspecialchars($_POST['message']); | |
| // 投稿処理 | |
| $res = $facebook -> api( | |
| '/me/feed', | |
| 'post', | |
| array('message' => $mes)); | |
| if($res){ | |
| echo "<script>alert('投稿しました');</script>"; | |
| }else{ | |
| echo "<script>alert('投稿できませんでした);</script>"; | |
| } | |
| } | |
| ?> | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>Facebook アプリ テスト版</title> | |
| </head> | |
| <body> | |
| <!-- メッセージ投稿 --> | |
| <?php if ($user): ?> | |
| <form action="" method="post"> | |
| <textarea name="message">メッセージを入力してください</textarea> | |
| <input type="submit" value="投稿"> | |
| </form> | |
| <?php else: ?> | |
| <a href="<?php echo $url; ?>" target="_TOP">Login</a> | |
| <?php endif ?> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment