Why is it not working on host server but on local?
The problem
Kreat is working like charm on localhost but on server nothing work
Environment
- PHP Version (
php -v): 7.2 - Firebase PHP SDK version (
composer show | grep kreait):
kreait/firebase-php 4.17.0 Firebase Admin SDK kreait/firebase-tokens 1.7.1 A library to work with Firebase tokens kreait/gcp-metadata 1.0.1 Get the metadata from a Google Cloud Platform environment.
Details
Kreait\Firebase\ServiceAccount\Discovery\FromEnvironmentVariable: The environment variable "FIREBASE_CREDENTIALS" is not set.\nKreait\Firebase\ServiceAccount\Discovery\FromEnvironmentVariable: The environment variable "GOOGLE_APPLICATION_CREDENTIALS" is not set.\nKreait\Firebase\ServiceAccount\Discovery\FromGoogleWellKnownFile: The well known file is not readable or invalid\ncURL error 6: Could not resolve host: metadata.google.internal (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)",
Code to reproduce issue
.env FIREBASE_CREDENTIALS=../bit1adminsdk.json
$firebase = (new Firebase\Factory())->create();
$database = $firebase->getDatabase();
$reference = $database->getReference('messages');
$reference->push(['bidId' => $bid->bidId]);
file bit1adminsdk has read permission and working in local as I said earlier
That's difficult to diagnose without know how the application is used and deployed on your server. You could try to debug it - in the file in which you have the code above, you could prepend:
# Check if environment variable is set
var_dump(getenv(FIREBASE_CREDENTIALS));
# If the environment variable is set, try to get the contents
echo file_get_contents(FIREBASE_CREDENTIALS);
# Try to get the service account directly, without using the env variable
$serviceAccount = ServiceAccount::fromJsonFile('../bit1adminsdk.json');
If you have a .env file, remember that you have to evaluate that file first with a Dotenv-Library like https://github.com/vlucas/phpdotenv (also shipped with Laravel) or the Symfony DotEnv component ( https://symfony.com/doc/current/components/dotenv.html )
Thank you!
This gets false string
var_dump(getenv(FIREBASE_CREDENTIALS));
it has been solved by using this line:
$serviceAccount = ServiceAccount::fromJsonFile('../bit1adminsdk.json');
Still confused why it does not read the .env variable
Does your application parse the environment variables file on your server? There must be a difference between your local and your server setup.
Do you use an application framework like Symfony or Laravel? Are other environment variables read? Is the .env file present on your server?
I’m closing this issue now because it’s not related to the library itself, but feel free to add further comments and share your findings so that it can help others having similar problems.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.