I'm trying to work with the Google Cloud Vision API with PHP. I created my first test page and when run from a website, the PHP works correctly with no issues. I have a new twist on my project and I want to run the same PHP code from the terminal.
The issue I'm having now though is that I get an error that my script doesn't have the credentials loaded.
Here is the code I'm using to load my credentials file that works in my website version...
putenv('GOOGLE_APPLICATION_CREDENTIALS=Credential-file.json');
This doesn't work on my Terminal run version of the code. So I've tried a couple other options like...
putenv('GOOGLE_APPLICATION_CREDENTIALS=/Full/Path/To/Credential-file.json');
and
$_ENV['GOOGLE_APPLICATION_CREDENTIALS'] = "/Full/Path/To/Credential-file.json";
The exact error is...
PHP Fatal error: Uncaught DomainException: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information in /Library/WebServer/Documents/google_vision/vendor/google/auth/src/ApplicationDefaultCredentials.php:156
My question is... Is there a better way to load the credentials file when I'm trying to run my PHP from the terminal? Or what mistake am I making in my version?
I should mention that I'm running this on MacOS with the Google Cloud client libraries.
Any help/suggestions would be greatly appreciated.
Thanks in advance!
The error message indicates that the variable is not set properly (as opposed to it being set to an invalid path).
putenv only lasts the duration of the current script. Setting values on $_ENV does not make them available to getenv, the method used by the Google auth library. Are you setting the credentials environment variable within the same script you're executing?
You can provide the value in the invocation as well:
GOOGLE_APPLICATION_CREDENTIALS=/path/to/keyfile.json php my-script.php
Or set the variable in your terminal session:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/keyfile.json
php my-script.php
If you're using Google\Cloud\Vision\V1\ImageAnnotatorClient, you can authenticate with a keyfile directly:
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
$vision = new ImageAnnotatorClient([
'credentials' => '/path/to/keyfile.json
]);
Or with Google\Cloud\Vision\VisionClient:
use Google\Cloud\Vision\VisionClient;
$vision = new VisionClient([
'keyFilePath' => '/path/to/keyfile.json'
]);
Related
I'm following the official docs to setup google cloud firestore to integrate with my php project
[1] https://cloud.google.com/firestore/docs/quickstart-servers
when I initialize the FirestoreClient I get the following error
Error rendering 'projects/{project=*}/databases/{database=*}': expected binding 'project' to match segment '{project=*}', instead got null\n
Provided bindings: Array\n
(\n
[project] => \n
[database] => (default)\n
)\n
First In the documentation it tells to use ENV VARIABLE though using the following command
export GOOGLE_APPLICATION_CREDENTIALS=/Users/user/Desktop/programming/workarea/.firebase.config.json
I investigated a lot and I think the problem happens when the library tries to establish a connection with the firestore server.
I couldn't find the root of the problem. but I think for some reason the php server not reading the env vars so it's not able to establish a proper connection
I struggled some time with this too, resolved it after realizing this: as said here, you first have to authenticate. For that, install the gcloud-sdk available here, then follow the steps provided.
Second, you have to set your environment variable with the path to your project's credentials file, as you were doing, in the bash (i use ~/.bashrc), exit the terminal and reenter it (necessary, as it may not apply the file edit for that terminal session). You could also type source .bashrc to apply changes without closing the terminal.
Then you should be fine, assuming you have the other required environment dependencies installed found here.
I've implemented a mail delivery service using SparkPost for a website. The code looks like this:
require '/vendor/autoload.php';
use SparkPost\SparkPost; use GuzzleHttp\Client;
use Ivory\HttpAdapter\Guzzle6HttpAdapter;
$httpAdapter = new Guzzle6HttpAdapter(new Client());
$sparky = new SparkPost($httpAdapter, ['key'=>'...']);
[...]
[...]
$results = $sparky->transmission->send($mailarray);
It works just fine locally on WampServer, however when I deploy it to Azure it does not. I don't have access to Azure logs, but I managed to narrow down the problem to one line:
$sparky = new SparkPost($httpAdapter, ['key'=>'...']);
I simply get a 500 error without any other explanation. The weird thing is when I wrap it around a try/catch, I still don't get anything other than a blank screen and a 500 on the console. I suspect it has to do something with /autoload.php not being able to load something.
Any thoughts?
According the requirement of SparkPost lib on Github repo at https://github.com/SparkPost/php-sparkpost/blob/master/composer.json#L18, it need PHP version higher than 5.5. So you can modify the PHP version of your Azure Web Apps, please refer to https://azure.microsoft.com/en-us/documentation/articles/web-sites-php-configure/#how-to-change-the-built-in-php-version for detail steps.
I'm trying to send an email using PHP from Google App Engine. To send an email, Require Once must include the php file Message.php: See: Code example here at Google
require_once 'google\appengine\api\mail\Message.php';
The Message.php file includes:
require_once 'google/appengine/api/mail/BaseMessage.php';
The BaseMessage.php file in turn requires three other PHP files:
require_once 'google/appengine/api/mail_service_pb.php';
require_once 'google/appengine/runtime/ApiProxy.php';
require_once 'google/appengine/runtime/ApplicationError.php';
So, to recap, Message.php requires BaseMessage.php and BaseMessage.php requires 3 more php files.
Everything works fine until that last PHP file that is required:
require_once 'google/appengine/runtime/ApplicationError.php';
Then a fatal error happens:
Fatal error: Class 'google\appengine\runtime\Error' not found in
C:\Users\UserName\Dropbox\ApplicationName\google\appengine\runtime\
ApplicationError.php on line 22
This is line 22:
class ApplicationError extends Error {
This is code that is provided by Google: the latest PHP SDK for App Engine
It's not code that I wrote. The path to the required files seems to be working, or else I'd get an error msg on the first required file. I've tested require_once lines of code that reference other PHP files in the same directories with no errors. So the issue isn't a problem with the relative path or the PHP.ini file.
My PHP version is Current PHP version: 5.4.22
Windows 7
XAMPP with Netbeans, Apache
How do I fix this problem?
I got the PHP email code to work. I still don't know what the exact problem was to begin with, but it must have something to do with how my Netbeans IDE is currently configured to PHP. The Google App Engine SDK has PHP built into it's installation, but I wasn't running my app from the Google App Engine Launcher. When I ran my app directly from the Google App Engine Launcher, the code worked. I'm guessing that the Netbeans version of PHP was a little different than the Google App Engine SDK version of PHP. And that caused an error in one of the files. In any case, I got the PHP code that sends a email to work. So, the code will run. That tells me that my configuration to PHP in Netbeans probably isn't the same as the configuration to PHP in the version of PHP in the Google App Engine SDK. I might try tweaking the PHP.ini file in netbeans to see if I can link directly to the Google App Engine SDK verion of PHP.
I am running PHP 5.3 on my local Windows 7 Laptop (have tried this all on our development server with no success, so I tried to see if I could get it to work on my laptop successfully first). When I call the following script:
$objAltovaXML = new COM('AltovaXML.Application');
I get the error:
Uncaught exception 'com_exception' with message 'Failed to create COM object `AltovaXML.Application': Access is denied.
I have checked using a PowerShell script I have found elsewhere on StackOverflow and have confirmed tht AltovaXML.Application is properly registered.
But how do I give my PHP script access to it?
While it should work I would just wrap a script around COM object and invoke that script via exec. Saves you a lot of trouble and is easy to debug separately and understand what exactly is going on.
I have an issue with obtaining data from Windows Azure's runtime using the newest (at the time of writing) PHP SDK from Github. Here is a test I am running on one of our hosted services:
<?php
include 'WindowsAzure/WindowsAzure.php';
use \WindowsAzure\ServiceRuntime\RoleEnvironment;
use \WindowsAzure\ServiceRuntime\Internal\RoleEnvironmentNotAvailableException;
try {
echo RoleEnvironment::getDeploymentId();
}
catch (RoleEnvironmentNotAvailableException $Exception) {
die('Failed to find deployment id');
}
RoleEnvironmentNotAvailableException is always thrown. Looking at the source, it seems to try sending commands through a named pipe (\.\pipe\WindowsAzureRuntime). Do I need to specify something within my ServiceConfiguration.csdef/cscfg in order to have access to this named pipe?
Any advice would be most welcome!
Got confirmation from MS EMEA Developer Support that current SDK does not support this function. They suggested similar workaround to jonnu above - use the previous SDK's functionality for role environment / configuration settings.
The ServiceRuntime APIs run only on Cloud so if this code snippet runs on local machine it'll throw an exception as you've pointed out. Further, if you want to debug your ServiceRuntime code you've to deploy your service to WA then use remote desktop connection to access the cloud machine and debug your code.