facebook api 中的 php cURL 错误

标签 php facebook curl

我使用php cURL获取 Facebook api 数据。这里有一些代码。第一cURL获取access_token然后查询下面的内容。但我收到错误 Warning: Invalid argument supplied for foreach()然后我回显 $body ,返回错误信息:

HTTP/1.1 400 Bad Request Cache-Control: no-store Content-Type: text/javascript; charset=UTF-8 Expires: Sat, 01 Jan 2000 00:00:00 GMT P3P: CP="Facebook does not have a P3P policy. Learn why here: <a href="http://fb.me/p3p" rel="noreferrer noopener nofollow">http://fb.me/p3p</a>" Pragma: no-cache WWW-Authenticate: OAuth "Facebook Platform" "invalid_request" "Unsupported post request." X-FB-Rev: 386815 Set-Cookie: datr=iOPoTeHtN_Q9AO5Y4LpEXwyV; expires=Sun, 02-Jun-2013 13:37:12 GMT; path=/; domain=.facebook.com; httponly X-FB-Server: 10.28.8.130 X-Cnection: close Date: Fri, 03 Jun 2011 13:37:12 GMT Content-Length: 79 {"error":{"type":"GraphMethodException","message":"Unsupported post request."}}

问题出在 cURL 中?谢谢。

php代码

$APPID='14017XXXXXXXXXX';
$APPSECRET='7702362a5f6d605882f1beXXXXXXXXXX';
$action="https://graph.facebook.com/oauth/access_token?client_id=".$APPID."&client_secret=".$APPSECRET."&grant_type=client_credentials" ;
$ch = curl_init($action);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST, true );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.1.1) Gecko/20090715 Firefox/3.5.1');
$r=curl_exec($ch);
$access_token=substr($r,13);
//echo $access_token; // echo 14017XXXXXXXXXX|LdTccOQAcHWrT2RCwXXXXXXXXXX
curl_close($ch);    
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/search?access_token=".$access_token."&q=sashimi&limit=20&type=page&scope=publish_stream,offline_access,user_status,read_stream");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$body = curl_exec($ch);
curl_close($ch);
$status_list = json_decode($body,true);
foreach ($status_list['data'] as $status_list) {   
 echo $status_list['id'];
}

json 树:

{
   "data": [
      {
         "name": "Sashimi",
         "category": "Food/beverages",
         "id": "61615549874"
      },
      {
         "name": "Sashimi",
         "category": "Restaurant/cafe",
         "id": "185314048174674"
      },
    ... ...  
   ],
   "paging": {
      "next": "https://graph.facebook.com/search?access_token=140174749371026|LdTccOQAcHWrT2RCwfVCqPVH0IY&q=SASHIMI&limit=20&type=page&scope=publish_stream\u00252Coffline_access\u00252Cuser_status\u00252Cread_stream&offset=20"
   }
}

最佳答案

问题是您正在执行 post 请求,但对于搜索,您需要执行 get 请求。 Curl 响应也表明了这一点。

{"error":{"type":"GraphMethodException","message":"Unsupported post request."}

只需删除行curl_setopt($ch, CURLOPT_POST, 1);。它会起作用并且对我有用:)。

关于facebook api 中的 php cURL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6227860/

相关文章:

php - Wordpress 上的特色图片

php - WordPress代码的干扰

php - 如何在 WooCommerce 中自动更新特定订单 ID 的订单状态

调用 Volley 时出现 PHP 500 内部服务器错误

javascript - 使用 JavaScript SDK 邀请 friend 加入 Facebook

javascript - Fb.api 在墙上发布未定义的帖子

facebook - 使用 koala 和 fb api 获取好友列表

linux - JSON 转义 CURL/JQ 输出

R:在 Rcurl getURL 语句中指定 SSL 版本

c++ - 多线程curl应用存在内存分配问题