2012-01-05 31 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);