dongqie2010 2011-06-03 13:43
浏览 129
已采纳

facebook api中的php cURL错误

I use php cURL to get facebook api data. some code here. first cURL get access_token then query something below. But I get error Warning: Invalid argument supplied for foreach() then I echo a $body , back error message:

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: http://fb.me/p3p" 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."}}

is the problem in cURL? Thanks.

php code

$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 tree:

{
   "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"
   }
}
  • 写回答

1条回答 默认 最新

  • dtpn60029 2011-06-03 14:16
    关注

    The problem is you are doing a post request but for search you need to do a get request. Curl response also showing that.

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

    Just remove line curl_setopt($ch, CURLOPT_POST, 1);. It will work and it worked for me :).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.