PHP Facebook SDK - fantasy0107/notes GitHub Wiki

Facebook SDK for PHP v5
Doc

安裝

composer require facebook/graph-sdk

composer-autoload?

範例

require_once __DIR__ . '/vendor/autoload.php'; // change path as needed

$fb = new \Facebook\Facebook([
  'app_id' => '{app-id}',
  'app_secret' => '{app-secret}',
  'default_graph_version' => 'v2.10',
  //'default_access_token' => '{access-token}', // optional
]);

// Use one of the helper classes to get a Facebook\Authentication\AccessToken entity.
//   $helper = $fb->getRedirectLoginHelper();
//   $helper = $fb->getJavaScriptHelper();
//   $helper = $fb->getCanvasHelper();
//   $helper = $fb->getPageTabHelper();

try {
  // Get the \Facebook\GraphNodes\GraphUser object for the current user.
  // If you provided a 'default_access_token', the '{access-token}' is optional.
  
  $response = $fb->get('/me', '{access-token}');
  
} catch(\Facebook\Exceptions\FacebookResponseException $e) {
  // When Graph returns an error
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
  // When validation fails or other local issues
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

$me = $response->getGraphUser();
echo 'Logged in as ' . $me->getName();

相關方法

Response

Response

  1. $response->getGraphUser()
  2. $response->getDecodedBody()

用 ssh 連線時碰到的問題

composer 下載時有時候會碰到需要輸入帳號密碼但是一直沒有過 或說下載失敗

原來我是用 ssh 連線所以需要再 Github 那邊新增 ssh public key
ssh config 底下也要增加host

測試 Github 連線

ssh -vT [email protected]

範例

    # Github
    Host github.com
    HostName github.com
    User Test
    IdentityFile ssh-path

參考

其它

Token 有什麼權限就可以藉由 Endpoint 拿甚麼樣的資料
facebook friend