2011-06-03 118 views
2

我使用php cURL來獲取facebook api數據。一些代碼在這裏。首先cURL得到access_token然後查詢下面的內容。但我得到的錯誤Warning: Invalid argument supplied for foreach()然後我贊同一個$body,回錯誤消息:php cURL在facebook api中的錯誤

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."}}

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" 
    } 
} 
+0

'var_dump($ status_list);' – zerkms 2011-06-03 13:47:21

+0

@zerkms still'HTTP/1.1 400 Bad Request Cache-Control:' – 2011-06-03 13:51:18

+0

:-S你要麼有json響應,要麼沒有。你有嗎? – zerkms 2011-06-03 13:59:33

回答

3

的問題是,你正在做一個POST請求,但搜索你需要做的GET請求。捲曲反應也表明了這一點。

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

只要刪除行curl_setopt($ch, CURLOPT_POST, 1);。它會工作,它爲我工作:)。

+0

對,刪除它效果很好。謝謝。 – 2011-06-03 14:43:09