【问题标题】:I want to post a notification with the facebook php sdk, how do I do this?我想用 facebook php sdk 发布通知,我该怎么做?
【发布时间】:2023-03-24 19:36:01
【问题描述】:

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

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

我找不到在线工作的示例,facebook api documentation 没有提供示例。

我的问题归结为:

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

S.

【问题讨论】:

  • 感谢您分享您的研究。尝试将问题更多地编辑为问题(即删除不赞成和“抱歉”的部分),并使问题本身更清晰。
  • 人们很可能会否决您的问题,因为它不包括您迄今为止尝试过的内容。换句话说,没有代码。
  • 好吧,如果最后我的代码有效并且我在发布问题时知道这一点,我将无法发布我的非工作代码。

标签: php facebook facebook-graph-api authorization facebook-php-sdk


【解决方案1】:

您需要一个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 获得的(归功于this question and answer)。

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

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

希望对其他人有用。

干杯,

S.

【讨论】:

  • 令牌只是 appid|appsecret
猜你喜欢
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 2017-01-18
  • 1970-01-01
  • 1970-01-01
  • 2018-01-16
  • 1970-01-01
  • 2020-05-24
相关资源
最近更新 更多