如何使用CURL在Facebook页面上发布帖子,而不使用PHP中的SDK
发布时间:2020-12-13 23:27:01 所属栏目:Linux 来源:网络整理
导读:我正在开发一个需要Facebook集成的 PHP项目.所以在我对代码进行处理之前,我正在使用Facebook Graph API资源管理器工具( https://developers.facebook.com/tools/explorer)对其进行测试.我现在在做的是. 1st Step Get the user access_token by using the but
我正在开发一个需要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,CURLOPT_POSTFIELDS,$data); curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE); curl_setopt($ch,CURLOPT_FOLLOWLOCATION,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 ) )
解决方法
为CURLOPT_POSTFIELDS传递数组意味着cURL会将请求作为Content-Type multipart / form-data发送 – 但您需要application / x-www-form-urlencoded.
在$data数组上使用http_build_query,并将结果字符串用于CURLOPT_POSTFIELDS. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |