我想 post 使用 facebook php sdk 发送通知,我该怎么做?

I want to post a notification with the facebook php sdk, how do I do this?

我有一个 Facebook 应用程序,我想发送通知。

我有用户的 ID,但似乎无法获得有效的访问令牌...

我找不到在线运行的示例,facebook api documentation 也没有提供示例。

我的问题归结为:

我如何获得访问令牌来执行此操作以及我可以使用哪个(有效)代码来执行此操作?

S.

您需要一个 app 访问令牌,幸好有一个页面可以让您获取它。

-> 保密,即不要在源代码管理中签入,它直接与您的应用程序秘密链接...

这是在撰写本文时有效的代码:

将示例中的 {test-user-id} 替换为用户 ID(例如测试用户)

<?php

session_start();

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

$fb = new Facebook\Facebook([
  'app_id' => '',
  'app_secret' => '',
  'default_graph_version' => 'v2.9',
  ]);

$token = ''; //see rest of answer

$message = 'You have people waiting to play with you, play now!';

$request = $fb->request('post', '/{test-user-id}/notifications?access_token='.$token.'&template='.$message.'&href=test.html');

// Send the request to Graph
try {
  $response = $fb->getClient()->sendRequest($request);
} 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 neverthelss: ' . $e->getMessage();
  exit;
}

$graphNode = $response->getGraphNode();

echo 'success: ' . $graphNode['success'] . ' error: ' . $graphNode['error'];

?>

$token是从this tool (credits to this question and answer)获得的。

页面输出succes: 1 error:(并将消息作为通知发送到测试用户帐户)。

如果您点击通知,您将被定向到 test.html 相对于您服务器上的应用根目录。

希望对大家有用

干杯,

S.