我想用facebook php sdk发布通知,我该怎么做?

问题描述 投票:-1回答:1

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

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

我找不到在线工作的例子,facebook api documentation 没有给出例子。

我的问题归结为:

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

S.

php facebook facebook-graph-api authorization facebook-php-sdk
1个回答
0
投票

你需要一个app访问令牌,幸运的是有一个页面,你可以得到它。

- >保密,即不要在源代码管理中签入,它与你的app秘密直接相关...

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

用用户id替换示例中的{test-user-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'];

?>

$令牌是从this toolthis question and answer的积分)获得的。

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

如果单击通知,则会相对于服务器上的应用程序根目录指向test.html

我希望它对其他人有用。

干杯,

S.

© www.soinside.com 2019 - 2024. All rights reserved.