Closed digitalStyx closed 6 years ago
If the project ID in your service account file doesn't match the database URI, you have to specify the database URI as documented in https://firebase-php.readthedocs.io/en/latest/setup.html#custom-database-uri :
$firebase = (new Factory())
->withServiceAccount($serviceAccount)
->withDatabaseUri(...)
->create();
You can find the URI for your database by navigating to the realtime database in the Firebase Web console and copying the value displayed there - it should be something in the form of https://xxxxxx.firebaseio.com/
.
I don't understand. I use generated file from the google console panel. How is it possible that Google generates incorrect data for me? Problem is during parsing JSON. I have very old project, and my project_id is not understandable for your code.
I have databaseUri: https://example.com:api-project-xxx.firebaseio.com and it is correct URI for google but it's not correct for this library and parse_url (PHP). The problem is in sign ":" .
I changed GuzzleHttp\Psr7/Uri for
public function __construct($uri = '')
{
// weak type check to also accept null until we can add scalar type hints
if ($uri != '') {
if ($uri == "https://example.com:api-project-xxx.firebaseio.com") {
$parts[0] = "https";
$parts[1] = "example.com:api-project-xxx.firebaseio.com";
} else {
$parts = parse_url($uri);
if ($parts === false) {
throw new \InvalidArgumentException("Unable to parse URI: $uri");
}
}
$this->applyParts($parts);
}
}
and it's work great, but it can't be. I think that not all URI for database is valid with RFC (parse_url), and it's a mistake to valid in this way
protected function getDatabaseUriFromServiceAccount(ServiceAccount $serviceAccount): UriInterface
{
return uri_for(sprintf(self::$databaseUriPattern, $serviceAccount->getProjectId()));
}
I see that firebase change url to database from:
https://example.com:api-project-xxx.firebaseio.com/ to https://example-com-api-project-xxx.firebaseio.com/
I think that something like that must be implemented for for project_id in form: example.com:api-project-xxx
The service account JSON file that Google generates is fine. It includes the project ID which can have any form, apperently.
In most projects, the project ID is identical to the subdomain on firebaseio.com, so a project id foo
would point to the Firebase Database URI https://foo.firebaseio.com
.
That's why the SDK by default uses the project ID from the service account file and uses it for the database URI.
In some project, the project ID is not identical to the subdomain on firebaseio.com, and that's why the ->withDatabaseUri()
method exists, so that you can override the value.
If you are sure that you are using the database URI displayed at https://console.firebase.google.com/u/0/project/_/database/_/data (after clicking the link, select your project) and the displayed URI indeed is https://example.com:api-project-xxx.firebaseio.com
, you should contact Google and tell them about this issue, because this is not a valid URI, which you can check with a cURL command in your terminal:
❯ curl -I "https://example.com:api-project-xxx.firebaseio.com/"
curl: (3) Port number ended with 'a'
This SDK, Guzzle and any HTTP Client need a valid URL to be able to connect to it. Even if you change Guzzle's source code, you will not be able to connect.
I see that firebase change url to database from:
https://example.com:api-project-xxx.firebaseio.com/ to https://example-com-api-project-xxx.firebaseio.com/
I think that something like that must be implemented for for project_id in form: example.com:api-project-xxx
This is something you can do when initializing the SDK:
$firebase = (new Factory())
->withServiceAccount($serviceAccount)
->withDatabaseUri('https://example-com-api-project-xxx.firebaseio.com')
->create();
Yep, if I use withDatabaseUri with my URI, then it's work great.
But I think, that library should do this.
anyway, thanks @jeromegamez .
I will try to look for a reliable specification by Google/Firebase - if there is one, I will 👍
Hello again @digitalStyx 😅
If you upgrade to the latest release, you should be able to remove the withDatabaseUri(...)
line again :). Cheers!
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.
I use version 4.13 with PHP 7.2.5 .
I have a project name in the form
"project_id": "example.com:api-project-xxxxxx",
and when I use example
I got error
Fatal error: Uncaught InvalidArgumentException: Unable to parse URI: https://example.com:api-project-xxxxxx.firebaseio.com in /export/www/DEVEL/example.com/vendor/guzzlehttp/psr7/src/Uri.php on line 78
What can be? example.com:api-project-xxxxxx is a valid name form for google