【问题标题】:PHP cURL : Post to Facebook Page as PagePHP cURL:作为页面发布到 Facebook 页面
【发布时间】:2015-04-06 06:02:16
【问题描述】:

我尝试将示例代码发布到 Facebook 页面。但它以我自己的身份发布。我已经包含了 Page_access_token。

授予的权限是“manage_pages”和“publish_action”。 这是我的代码:

<?php

    $page_id='xxxx';
    $page_access_token='cccc';
    $url="https://graph.facebook.com/{$page_id}/feed?message=Hello&access_token=".$page_access_token;
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_REFERER, '');
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);

    $value = json_decode(curl_exec($ch));
    $var_dump($value);

?>

我哪里错了?我该如何解决? 我希望它作为页面发布。谢谢

编辑: 我的 /$PAGE_ID?fields=access_token 的新代码:

<?php
  $page_id='xxx';
  $message='helloworld';
  $url="https://graph.facebook.com/v2.3/{$page_id}?fields=access_token";
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_REFERER, '');
  curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
  curl_setopt($curl, CURLOPT_AUTOREFERER, true);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $message);
  curl_setopt($curl, CURLOPT_TIMEOUT, 10);

  $json = json_decode(curl_exec($curl));
  var_dump($json);
?>

这会返回一个错误:

object(stdClass)#1 (1) {
  ["error"]=>
  object(stdClass)#2 (3) {
    ["message"]=>
    string(64) "(#210) A page access token is required to request this resource."
    ["type"]=>
    string(14) "OAuthException"
    ["code"]=>
    int(210)
  }
}

我哪里错了?

我需要从图形 API 资源管理器创建 page_access_token 吗?还是上面的网址就足够了?我应该如何使用 User_access_token 来获取 page_access_token ?

【问题讨论】:

  • 你会得到什么回报?
  • 它返回一个 ID,类似于 stdClass 对象 ( [id] => 1387602394895539_1390602811262164 ) 消息被发布到页面,但我自己不是页面

标签: php facebook facebook-graph-api curl


【解决方案1】:

如果它是以您自己的身份发布的,则您没有使用页面令牌。调试您的令牌并查看是否列出了页面 ID - 这就是您知道它是页面令牌的方式:https://developers.facebook.com/tools/debug/

顺便说一句,您应该使用新的publish_pages 权限来发布到页面:https://developers.facebook.com/docs/apps/changelog#v2_3_changes

有关如何获取访问令牌的信息:

【讨论】:

  • 我也尝试了“manage_pages”和“publish_pages”。如果我只使用这两个,什么都不会发布。如何获得页面令牌?我需要用户令牌来获取页面令牌吗?
  • 这是一个很棒的链接。但是我怎样才能用 cURL 来实现呢?
  • 只需使用 curl 对“/PAGE-ID?fields=access_token”进行 api 调用以获取页面令牌。
  • 好的。我试过这个。但它给我一个错误。我已经发布了我的新代码。请检查并为我提供解决方案
  • 我真的不知道如何实现它,因为我只是处于学习阶段。我知道的所有可能的方法都试过了。如果你能提供一个代码 sn-p 对我来说将是一个很大的帮助。谢谢
【解决方案2】:
    <?php
  $page_id='xxx';
  $message='helloworld';
$access_token = "XXXXXXXXXXXXXXXXXXXXX";
  $url="https://graph.facebook.com/v2.3/{$page_id}/feed/?access_token=".access_token ."&message=".urlencode(message);
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_REFERER, '');
  curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
  curl_setopt($curl, CURLOPT_AUTOREFERER, true);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $message);
  curl_setopt($curl, CURLOPT_TIMEOUT, 10);

  $json = json_decode(curl_exec($curl));
  var_dump($json);
?>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-08
  • 2017-02-05
相关资源
最近更新 更多