I am getting the following error:
Fatal error: Class 'Google\Cloud\Datastore\DatastoreClient' not found in /base/data/home/apps/e~brookes-room-usage/1.399853826395726238/record-usage.php on line 84
so presumably I have failed to point my app correctly to that class.
I have enabled the Datastore API on the cloud console.
I have tried to find documentation relating to how to set up a PHP connection to the Datastore API.
I tried to create a settings.yml file using the instructions here but I didn't know what my client ID or my client secret were.
# Google credentials and configuration
google_client_id: YOUR_CLIENT_ID
google_client_secret: YOUR_CLIENT_SECRET
google_project_id: YOUR_PROJECT_ID
# options are "cloudsql", "mongodb", or "datastore"
bookshelf_backend: datastore
(source on Github)
What do I need to get my PHP app on Google Cloud to recognise Google\Cloud\Datastore\DatastoreClient and connect to the Datastore API so that I can retrieve and post data?
I think you forgot to include google/cloud-datastore library. Install via composer as below.
$ composer require google/cloud-datastore
and you can use as below.
<?php
require 'vendor/autoload.php';
use Google\Cloud\Datastore\DatastoreClient;
$datastore = new DatastoreClient([
'projectId' => 'my_project'
]);
// Create an entity
$bob = $datastore->entity('Person');
$bob['firstName'] = 'Bob';
$bob['email'] = 'bob#example.com';
$datastore->insert($bob);
// Update the entity
$bob['email'] = 'bobV2#example.com';
$datastore->update($bob);
// If you know the ID of the entity, you can look it up
$key = $datastore->key('Person', '12345328897844');
$entity = $datastore->lookup($key);
More details: https://github.com/GoogleCloudPlatform/google-cloud-php#google-cloud-datastore-ga
I am already using this and working fine for me.
So it turns out that you can only run PHP with Datastore on a Google Cloud app on your local device, not on Google Appengine, unless you use a third-party library.
Which is why I am now writing my app in Python instead.
(Update: this may no longer be the case. This answer was written in 2017.)
Related
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'
]);
I am having really difficult time trying to connect Google Storage. All I need is to be able to upload PDF file to a bucket that I've created on Google Storage Console. The documentation seems to be all over the place and lacking simple examples of PHP code. So here's what I've done so far:
Installed cloud storage
$ composer require google/cloud-storage
Added billing as per Google's requirement. Enabled Cloud Storage API. Created project and added a bucket.
Attempted to use the following example:
require '../vendor/autoload.php';
define("PROJECT_ID", "my-project");
define("BUCKET_NAME", "my-bucket");
$client = new Google_Client();
$client->setApplicationName("API_Cloud_Storage");
$client->useApplicationDefaultCredentials();
$client->setScopes(["https://www.googleapis.com/auth/cloud-platform"]);
$service = new Google_Service_Storage($client);
$request = $service->buckets->listBuckets(PROJECT_ID);
foreach ($request["items"] as $bucket)
printf("%s\n", $bucket->getName());
Keep on getting error on
Fatal error: Uncaught Error: Class 'Google_Client' not found in /home/domain/public_html/test.php:11 ...
I know the vendor/autoload.php file is loading because I have no issues with AWS in a different script.
I didn't even pass the first line. What am I missing?
You have the documentation page for Cloud Storage Client Libraries with a PHP sample and detailed instructions. There's also a link to the GitHub repo for that quickstart, in addition to the one shared by ceejayoz in the comments.
I am just trying to do a very simple file_get_contents('gs://[bucket][file]');
I am getting this error when I run locally.
"\google\appengine\ext\cloud_storage_streams\CloudStorageStreamWrapper::stream_open" call failed.
However, when I run it on my production env it works fine.
I know I am missing something with the setup, and I am just not sure what.
I have set the project gcloud config set project PROJECT_ID, set the account gcloud config set account ACCOUNT, and run gcloud auth login. Any ideas on what I am missing in my setup here?
I ran composer install , composer require google/cloud, I don't get any errors when including use Google\Cloud\Storage\StorageClient; I created the service-account-keyfile as an owner of the bucket. Set owners to full permissions. Ran this code...
require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
$storage = new StorageClient([
'keyFilePath' => __DIR__ .'/../keys/appdocs-com.json',
]);
echo file_get_contents('gs://[myBucket][myFile]');
and I am still getting ...streamWrapper::stream_open" call failed
I even tried the service builder approach.
require __DIR__ . '/vendor/autoload.php';
use Google\Cloud\ServiceBuilder;
use Google\Cloud\Storage\StorageClient;
$gcloud = new ServiceBuilder([
'keyFilePath' => __DIR__ .'/../keys/appdocs-com.json',
'projectId' => 'appdocs-com',
]);
$storage = $gcloud->storage();
$bucket = $storage->bucket('test-appdocs-sendgrid-inbound');
$object = $bucket->object('4/envelope.json');
$stream = $object->downloadAsStream();
echo $stream->getContents();
this returns
Fatal error: Uncaught exception 'Google\Cloud\Core\Exception\ServiceException' with message 'No system CA bundle could be found in any of the the common system locations. PHP versions earlier than 5.6 are not properly configured to use the system's CA bundle by default. In order to verify peer certificates, you will need to supply the path on disk to a certificate bundle to the 'verify' request option: http://docs.guzzlephp.org/en/latest/clients.html#verify. If you do not need a specific certificate bundle, then Mozilla provides a commonly used CA bundle which can be downloaded here (provided by the maintainer of cURL): https://raw.githubusercontent.com/bagder/ca-bundle/master/ca-bundle.crt. Once you have a CA bundle available on disk, you can set the 'openssl.cafile' PHP ini setting to point to the path to the file, allowing you to omit the 'verify' request option. See http://curl.haxx.se/docs/sslcerts.html for more information.' in /Users/thom/Engine/appdocs-com/vendor/google/cloud-core/RequestWrapper.php:241 Stack trac in /Users/thom/Engine/appdocs-com/vendor/google/cloud-core/RequestWrapper.php on line 241
I have my runtime at php55. Could that be preventing me from using StorageClient or serviceBuilder
When outside the appengine environment, you will need to manually register the cloud storage stream wrapper to access files using the gs:// protocol:
use Google\Cloud\Storage\StorageClient;
$storage = new StorageClient([
'keyFilePath' => '/path/to/service-account-keyfile.json'
]);
$storage->registerStreamWrapper();
This example assumes you have Google Cloud PHP installed.
More information is available in the Google Cloud PHP docs.
To resolve the guzzle ssh error, refer to this answer. NOTE that setting verify to false is not safe, and should only be used to test, never in production.
As #jdp mentioned in his answer, the error I was getting was related to my installation of curl.
So I looked at Google's php_ini docs to see if I could set the openssl.cafile. https://cloud.google.com/appengine/docs/standard/php/config/php_ini
If you want to use Google Storage Client you need to change from standard to flexible env.
https://cloud.google.com/appengine/docs/flexible/php/quickstart
Then read through cloud storage docs to see how to download dependencies for the API usage: https://cloud.google.com/storage/docs/reference/libraries
Once you have set up your flex env and gathered dependencies, you can then follow these directions to set up the connection.
Set up a service account key In Google Cloud Console:
Select "your-project"->IAM & ADMIN-> Service Accounts.
Click the "Create Service Account" button.
Assign role (in my case I assigned the role "owner" since this service account was specific to my local development), mark the checkbox "Furnish New Private Key".
This just downloaded your "service-account-keyfile.json"
Then in the code initiate Cloud Storage like so:
use Google\Cloud\Storage\StorageClient;
$storage = new StorageClient([
'keyFilePath' => '/path/to/service-account-keyfile.json'
]);
$storage->registerStreamWrapper();
You can now retrieve bucket data like you would from the production env.
echo file_get_contents('gs://[Bucket][File]');
Make sure you have set the bucket permissions to allow the previously selected role to access the bucket.
However, I would still love to find a way to set up a local environment with with php standard that can interact with gs://[bucket]/[file].
I am developing an oAuth 2.0 server using thephpleague library provided by Alex Bilbie. But, after the initializing the authorization server, when I declare the storage classes it throws an following error.
"Fatal error: Class 'Storage\SessionStorage' not found"
Please help me to resolve this. I read your post about this problem here:- Guide me implementing Oauth2 PHP server using thephpleague library
Please let me know how can I implement the storage classes. My current code:
require_once "/../vendor/autoload.php";
$server = new \League\OAuth2\Server\AuthorizationServer;
$server->setSessionStorage(new Storage\SessionStorage);
$server->setAccessTokenStorage(new Storage\AccessTokenStorage);
$server->setClientStorage(new Storage\ClientStorage);
$server->setScopeStorage(new Storage\ScopeStorage);
$server->setAuthCodeStorage(new Storage\AuthCodeStorage);
$authCodeGrant = new \League\OAuth2\Server\Grant\AuthCodeGrant();
$server->addGrantType($authCodeGrant);
The library that you use requires implementing your own storage classes, see http://oauth2.thephpleague.com/implementing-storage-interfaces/. The class names that you use are from an example implementation https://github.com/thephpleague/oauth2-server/tree/master/examples/relational/Storage that uses Capsule as its storage backend. If you want to use Capsule as you backend, you'll need to download those example implementation classes and install https://github.com/illuminate/database.
IMPORTANT EDIT: Found out that issue is caused by my web server (I don't know how to fix that) - tags changed. I use latest XAMPP.
I am trying to set up server to server connection with Google Analytics API with OAuth following this guide:
Service Applications and Google Analytics API V3: Server-to-server OAuth2 authentication?
I have following code:
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_AnalyticsService.php';
// create client object and set app name
$client = new Google_Client();
$client -> setApplicationName("------------"); // name of your app
// set assertion credentials
$client->setAssertionCredentials(
new Google_AssertionCredentials(
"------------#developer.gserviceaccount.com", // email you added to GA
array('https://www.googleapis.com/auth/analytics.readonly'),
file_get_contents("-:/-----------/-----/------/----/---------------------------------------------------.p12") // keyfile you downloaded
));
// other settings
$client->setClientId("------------.apps.googleusercontent.com"); // from API console
$client->setAccessType('offline_access'); // this may be unnecessary?
// create service and get data
$service = new Google_AnalyticsService($client);
$ids = "ga:--------";
$startDate = "2013-05-01";
$endDate = "2013-05-26";
$metrics = "ga:visitors";
var_dump($service->data_ga->get($ids, $startDate, $endDate, $metrics));
Which causes HTTP error #101 (ERR_CONNECTION_RESET). My issue is similar to: Service Applications and Google Analytics API V3: Error 101 (net::ERR_CONNECTION_RESET) but I have no other calls to Google_AnalyticsService().
I have PHP version 5.4.7 (XAMPP version) with enabled OpenSSL (enabled via uncommeting line in php.ini).
Thank you in advance for any answers. How to deal with ones like this (producing some HTTP error and stopping? Are there logs that produce any useful informations?)?
EDIT: Copying code from post: "Not sufficient permissions" google analytics API service account to me causes same error (no other messages printed).
I found way to deal with this issue. It's caused by Apache and PHP together - they use different version of OpenSSL.
First workaround (one that does not work for me as I write it 05-28-3013) on XAMPP 1.8.1 (release date 30.9.2012) is to replace files ssleay32.dll and libeay32.dll within Apache /bin/ folder with these from php folder. It does not work for me because mod_ssl can't work with PHP's DLLs.
Second workaround is to install other package. Personally, I switched to ZendServer which has synced versions of OpenSSL and Apache.
Of course you can always build your own Apache and PHP making sure you have OpenSSL and mod_ssl cooperating and in latest version.