2
votes

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

3

3 Answers

2
votes

I suggest you use the Google API PHP library http://code.google.com/p/google-api-php-client/ It has built-in methods for updating your models.

It will save you alot of trouble.

0
votes

In this line:

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

It looks like you're missing a forward slash after the ID and before the question mark. Try this instead:

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

I've found the solution. here is the code that need changes:

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