【问题标题】:How to publish a post on Facebook Page using CURL and without using SDK in PHP如何使用 CURL 在 Facebook 页面上发布帖子,而不使用 PHP 中的 SDK
【发布时间】:2015-09-04 06:04:31
【问题描述】:

我正在开发一个需要 Facebook 集成的 PHP 项目。因此,在我对代码执行此操作之前,我正在使用 Facebook Graph API 资源管理器工具 (https://developers.facebook.com/tools/explorer) 对其进行测试。我现在正在做的是。

第一步

Get the user access_token by using the button at the top left.

第二步

Make a GET request to "me/accounts" to get the page token and page id.

第三步

Make a POST request to "{page_id}/feed" with the fields message={message} and access_token={page_token}

它运行良好并发布在我的 Facebook 粉丝页面上。但是当我尝试用这样的 PHP 代码替换“第三步”时

$data['message'] = "my message";


$data['access_token'] = $page_access_token; //page token from 2nd step
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, 'https://graph.facebook.com/{page_id}/feed');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, 1);


$resp = curl_exec($ch);

curl_close($ch);

$data_resp = json_decode($resp);

print_r($data_resp);

它显示了这个错误。

stdClass Object ( [error] => stdClass Object ( [message] => (#200) Permissions error [type] => OAuthException [code] => 200 ) ) 

我设置了manage_pages、publish_pages、publish_actions的权限

【问题讨论】:

标签: php facebook facebook-graph-api curl


【解决方案1】:

CURLOPT_POSTFIELDS 传递一个数组意味着 cURL 将以 Content-Type multipart/form-data 的形式发送请求——但您需要 application/x-www-form-urlencoded

在您的$data 数组上使用http_build_query,并将生成的字符串用于CURLOPT_POSTFIELDS

【讨论】:

    【解决方案2】:

    查看文档

    权限

    • 具有publish_actions 权限的用户访问令牌可用于代表该人发布新帖子。帖子将以用户的声音出现。
    • 具有publish_pages 权限的页面访问令牌可用于代表该页面发布新帖子。帖子将出现在页面的声音中。

    您应该通过

    运行使用过的访问令牌

    查看它是否包含适当的权限。

    【讨论】:

    • 是的。令牌很好,它包含所需的所有权限。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 2016-08-25
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多