2012-01-05 58 views
2

我試圖在php中更新我的模型。我可以訓練和預測,但我無法更新。也許這是因爲使用PUT,但我找不到問題。下面是代碼:在PHP中更新Google預測模型

$authCode = $_GET['token']; 
$man =$_GET['owner']; 
$type = $_GET['type']; 
$title= $_GET['title']; 
$id = "*****"; 

$api_key = "**********************"; 
$url = "https://www.googleapis.com/prediction/v1.4/trainedmodels/".$id."?pp=1&key=".$api_key; 
$header = array('Content-Type:application/json','Authorization: OAuth '.$authCode); 


$str = "label=dislike&csvInput[]=video&csvInput[]=war&csvInput[]=john"; 
parse_str($str, $output); 

$putData = tmpfile(); 
fwrite($putData, $output); 
fseek($putData, 0);  

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_PUT, true); 
curl_setopt($ch, CURLOPT_INFILE, $putData); 
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($output)); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

$ss = curl_exec($ch); 
curl_close($ch); 
echo(print_r($ss)); 

響應:

HTTP/1.1 400錯誤的請求內容類型:應用/ JSON; charset = UTF-8 Date:Sat,07 Jan 2012 19:25:32 GMT過期時間:星期六,07一月2012 19:25:32 GMT Cache-Control:private,max-age = 0 X-Content-Type-Options: nosniff X-Frame-Options:SAMEORIGIN X-XSS-Protection:1; mode = block Server:GSE Transfer-Encoding:chunked {「error」:{「errors」:[{「domain」:「global」,「reason」:「parseError」,「message」:「Parse Error」}] 「代碼」:400,「消息」:「語法錯誤」}}

回答

0

在這一行:

$url = "https://www.googleapis.com/prediction/v1.4/trainedmodels/".$id."?pp=1&key=".$api_key; 

它看起來像你錯過了ID之後,問題之前斜槓標記。試試這個:

$url = "https://www.googleapis.com/prediction/v1.4/trainedmodels/".$id."/?pp=1&key=".$api_key; 
+0

不,不是這樣。它仍然給出相同的錯誤響應。 – Calado 2012-01-06 13:39:15

+0

您是否嘗試過使用[API資源管理器](http://code.google.com/apis/explorer/#_s=prediction&_v=v1.4&_m=trainedmodels.update)來查看您的JSON編碼數據是否合格? – Peter 2012-01-06 17:29:35

+0

是的,通常我會嘗試那裏的樣品。在資源管理器中,這個例子有效我把代碼放在$ jsonData的回聲之上,你可以看到JSON文件是正確的! – Calado 2012-01-06 17:55:21

0

我找到了解決方案。這裏要說的是需要改變的代碼:您使用谷歌API PHP庫

$output = json_encode($requestBody); 
$putData = tmpfile(); 
fwrite($putData, $output); 
fseek($putData, 0); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_PUT, true); 
curl_setopt($ch, CURLOPT_INFILE, $putData); 
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($output)); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);