Skip to content

Instantly share code, notes, and snippets.

@shoma
Last active November 5, 2016 16:39
Show Gist options
  • Save shoma/e64d0ecb9d3aa0673114 to your computer and use it in GitHub Desktop.
Save shoma/e64d0ecb9d3aa0673114 to your computer and use it in GitHub Desktop.
An example usage for Google Cloud Vision API with PHP on GCE
{
"require": {
"google/apiclient": "2.x-dev",
"google/appengine-php-sdk": "^1.9"
}
}
<?php
require './vendor/autoload.php';
use Google_Client;
use Google_Service_Vision;
use Google_Service_Vision_AnnotateImageRequest;
use Google_Service_Vision_BatchAnnotateImagesRequest;
use Google_Service_Vision_Feature;
use Google_Service_Vision_Image;
use Google_Service_Vision_ImageSource;
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes([Google_Service_Vision::CLOUD_PLATFORM]);
$service = new Google_Service_Vision($client);
$body = new Google_Service_Vision_BatchAnnotateImagesRequest();
$feature = new Google_Service_Vision_Feature();
$feature->setType('LABEL_DETECTION');
$feature->setMaxResults(100);
$src = new Google_Service_Vision_ImageSource();
$src->setGcsImageUri($gcsurl);
$image = new Google_Service_Vision_Image();
$image->setSource($src);
$payload = new Google_Service_Vision_AnnotateImageRequest();
$payload->setFeatures($features);
$payload->setImage($image);
$body->setRequests([$payload]);
/** @var $res \Google_Service_Vision_BatchAnnotateImagesResponse */
$res = $service->images->annotate($body, $optParams);
@noxtras
Copy link

noxtras commented Oct 8, 2016

Hello,
Thank you for the example. Could you please tell me what goes into $optParams? It gives me an error: Warning: array_merge(): Argument #2 is not an array in ../vendor/google/apiclient-services/src/Google/Service/Vision/Resource/Images.php on line 38

@alexmgillis
Copy link

Thank you very much! this is very informative.
I think you have a typo in line 25 I think it should be $feature (in singular)
Also, I try to reproduce this script in my platform (Laravel + google/apiclient) and I keep gettign this error:

Error calling POST https://vision.googleapis.com/v1/images:annotate: (403) Request had insufficient authentication scopes.

Do you have any idea why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment