Created
May 12, 2019 07:55
-
-
Save epynic/3f20f2c1e8acdcd3bea8f8b4bd388c13 to your computer and use it in GitHub Desktop.
CURL sample request to AutoML REST API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$image = $_POST['image']; | |
$key = trim(shell_exec('sudo -u service_name gcloud auth application-default print-access-token 2<&1')); // replace service_name with your service account name | |
$post='{"payload":{"image":{"imageBytes": "'.$image.'"}}}'; | |
$curl = curl_init(); | |
// The REST API URL can be found from the predict tab example replace with your account URL https://automl.googleapis.com/v1beta1/projects/quizappflutter/locations/us-central1/models/ICNXXXXXXXXXX41:predict | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => "https://automl.googleapis.com/v1beta1/projects/quizappflutter/locations/us-central1/models/ICNXXXXXXXXXX41:predict", | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => "", | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 30, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => "POST", | |
CURLOPT_POSTFIELDS => $post, | |
CURLOPT_HTTPHEADER => array( | |
"Authorization: Bearer $key", | |
"Content-Type: application/json", | |
), | |
)); | |
$response = curl_exec($curl); | |
$err = curl_error($curl); | |
curl_close($curl); | |
if ($err) { | |
echo "cURL Error #:" . $err; | |
} else { | |
echo $response; | |
} |
not sure but it should work, you have also activated your service account right ?
gcloud auth activate-service-account --key-file=key.json
https://stackoverflow.com/questions/43278622/gcloud-auth-activate-service-account-error-please-ensure-provided-key-file-is
Hi there, anyone tried this in production on a live server?
I'm battling to get the token, my error: "Uncaught Error: Object of class Google\Cloud\Core\ServiceBuilder could not be converted to string"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
According to what I did for this case. In order to able access the curl via ssh or terminal, you must run this code below,
export GOOGLE_APPLICATION_CREDENTIALS="key-path.json"
Then I tried to run it in php with your code. I did all your steps in medium, and even try all methods i get from google. The only thing i get from the output since a week ago until now '"Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. '