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

Calado
  • 39
  • 1
  • 7

3 Answers3

2

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.

Whales
  • 388
  • 1
  • 3
  • 10
0

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;
Peter
  • 2,526
  • 1
  • 23
  • 32
  • Did you try using the [APIs explorer](http://code.google.com/apis/explorer/#_s=prediction&_v=v1.4&_m=trainedmodels.update) just to see if your JSON encoded data passes muster? – Peter Jan 06 '12 at 17:29
  • yes, normally I try the samples there. In explorer, this example works. I put in the code above the echo of $jsonData and you can see that the JSON file it's correct! – Calado Jan 06 '12 at 17:55
  • If you are able to use the APIs explorer to update your model successfully then there's obviously nothing wrong with your model (i.e. must be a Categorization model) or your data (which certainly looks fine). The only other thing that occurs to me is that there might be some confusion of the raw PUT data and the Query string (which contains the "pp" and "key" parameters) - if you had an OAuth2 token you could test this by passing the "Authorization: Bearer {token}" header in the request and dropping everything after the $id from your $url string. – Peter Jan 06 '12 at 18:41
  • Yes, I have the OAuth 2.0 token but that way it doesn't work either. I put that "pp" and "key" because it appears in APIs explorer.I also think that the problem is in PUT, because I have train.php and predict.php working with POST and they work fine. – Calado Jan 06 '12 at 18:57
  • So now I've changed the way to encode the message to send as request and also changed the curl settings. Now the error changed from "Update data invalid" to "Parse Error"! – Calado Jan 07 '12 at 19:31
0

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);
Calado
  • 39
  • 1
  • 7