2

I'm trying to update my model in php. I can train and predict but I can't update. Maybe it's because of the use of PUT but I can't find the problem. Here is the code:

$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));

Response:

HTTP/1.1 400 Bad Request Content-Type: application/json; charset=UTF-8 Date: Sat, 07 Jan 2012 19:25:32 GMT Expires: Sat, 07 Jan 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" } ], "code": 400, "message": "Parse Error" } }

4

3 回答 3

2

我建议您使用 Google API PHP 库http://code.google.com/p/google-api-php-client/ 它具有用于更新模型的内置方法。

它将为您省去很多麻烦。

于 2013-04-23T11:47:59.920 回答
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;
于 2012-01-06T01:57:52.327 回答
0

我找到了解决方案。这是需要更改的代码:

$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);
于 2012-01-07T19:41:45.687 回答