kreait / firebase-php

Unofficial Firebase Admin SDK for PHP
https://firebase-php.readthedocs.io/
MIT License
2.25k stars 429 forks source link

syntax error, unexpected '|', expecting variable (T_VARIABLE) #685

Closed carinihdev closed 2 years ago

carinihdev commented 2 years ago

Describe the issue you are experiencing

Hello all, I encountered an issue with Cloud Messaging.

$messaging->send($message); --> This function result in the following error:

An uncaught Exception was encountered Type: Kreait\Firebase\Exception\Messaging\MessagingError Message: syntax error, unexpected '|', expecting variable (T_VARIABLE) Filename: ../vendor/kreait/firebase-php/src/Firebase/Exception/MessagingApiExceptionConverter.php Line Number: 56

I used the following codes:

use Kreait\Firebase\Factory; use Kreait\Firebase\Messaging\CloudMessage; use Kreait\Firebase\Messaging\Notification;

$factory = (new Factory)->withServiceAccount('path to sdk json'); $messaging = $factory->createMessaging(); $message = CloudMessage::withTarget('token', 'my device token') ->withNotification(Notification::create('Title', 'Body'));
$messaging->send($message);

Error occur when it reach this function --> $messaging->send($message);

I have also tried with kreait/firebase v6. Same error occur.

Environment: PHP 7.4.3. kreait/firebase-php 5.26.1 Framework Codeigniter 3.1.11 with composer autoload.

Thanks for the help.

Installed packages

chriskacerguis/codeigniter-restserver 3.1.2 CI Rest Server fig/http-message-util 1.1.5 Utility classes and constants... firebase/php-jwt v5.5.1 A simple library to encode an... google/auth v1.18.0 Google Auth Library for PHP google/cloud-core v1.43.2 Google Cloud PHP shared depen... google/cloud-storage v1.26.0 Cloud Storage Client for PHP google/crc32 v0.1.0 Various CRC32 implementations guzzlehttp/guzzle 7.4.1 Guzzle is a PHP HTTP client l... guzzlehttp/promises 1.5.1 Guzzle promises library guzzlehttp/psr7 2.1.0 PSR-7 message implementation ... kreait/clock 1.1.0 A PHP 7.0 compatible clock ab... kreait/firebase-php 5.26.1 Firebase Admin SDK kreait/firebase-tokens 1.16.1 A library to work with Fireba... lcobucci/clock 2.1.0 Yet another clock abstraction lcobucci/jwt 4.1.5 A simple library to work with... monolog/monolog 2.3.5 Sends your logs to files, soc... mtdowling/jmespath.php 2.6.1 Declaratively specify how to ... psr/cache 2.0.0 Common interface for caching ... psr/http-client 1.0.1 Common interface for HTTP cli... psr/http-factory 1.0.1 Common interfaces for PSR-7 H... psr/http-message 1.0.1 Common interface for HTTP mes... psr/log 3.0.0 Common interface for logging ... psr/simple-cache 1.0.1 Common interfaces for simple ... ralouphie/getallheaders 3.0.3 A polyfill for getallheaders. riverline/multipart-parser 2.0.9 One class library to parse mu... rize/uri-template 0.3.4 PHP URI Template (RFC 6570) s... symfony/deprecation-contracts v3.0.0 A generic function and conven... symfony/polyfill-mbstring v1.24.0 Symfony polyfill for the Mbst... symfony/polyfill-php80 v1.24.0 Symfony polyfill backporting ... symfony/polyfill-php81 v1.24.0 Symfony polyfill backporting ..

PHP version and extensions

composer-plugin-api 1.1.0 The Composer Plugin API ext-bz2 7.4.3 The bz2 PHP extension ext-calendar 7.4.3 The calendar PHP extension ext-ctype 7.4.3 The ctype PHP extension ext-curl 7.4.3 The curl PHP extension ext-date 7.4.3 The date PHP extension ext-dom 20031129 The dom PHP extension ext-exif 7.4.3 The exif PHP extension ext-FFI 7.4.3 The FFI PHP extension ext-fileinfo 7.4.3 The fileinfo PHP extension ext-filter 7.4.3 The filter PHP extension ext-ftp 7.4.3 The ftp PHP extension ext-gd 7.4.3 The gd PHP extension ext-gettext 7.4.3 The gettext PHP extension ext-hash 7.4.3 The hash PHP extension ext-iconv 7.4.3 The iconv PHP extension ext-json 7.4.3 The json PHP extension ext-libxml 7.4.3 The libxml PHP extension ext-mbstring 7.4.3 The mbstring PHP extension ext-mysqli 7.4.3 The mysqli PHP extension ext-mysqlnd 0 The mysqlnd PHP extension (actual version: mys... ext-openssl 7.4.3 The openssl PHP extension ext-pcntl 7.4.3 The pcntl PHP extension ext-pcre 7.4.3 The pcre PHP extension ext-PDO 7.4.3 The PDO PHP extension ext-pdo_mysql 7.4.3 The pdo_mysql PHP extension ext-Phar 7.4.3 The Phar PHP extension ext-posix 7.4.3 The posix PHP extension ext-readline 7.4.3 The readline PHP extension ext-Reflection 7.4.3 The Reflection PHP extension ext-session 7.4.3 The session PHP extension ext-shmop 7.4.3 The shmop PHP extension ext-SimpleXML 7.4.3 The SimpleXML PHP extension ext-sockets 7.4.3 The sockets PHP extension ext-sodium 7.4.3 The sodium PHP extension ext-SPL 7.4.3 The SPL PHP extension ext-sysvmsg 7.4.3 The sysvmsg PHP extension ext-sysvsem 7.4.3 The sysvsem PHP extension ext-sysvshm 7.4.3 The sysvshm PHP extension ext-tokenizer 7.4.3 The tokenizer PHP extension ext-xml 7.4.3 The xml PHP extension ext-xmlreader 7.4.3 The xmlreader PHP extension ext-xmlwriter 7.4.3 The xmlwriter PHP extension ext-xsl 7.4.3 The xsl PHP extension ext-Zend-OPcache 7.4.3 The Zend OPcache PHP extension ext-zip 1.15.6 The zip PHP extension ext-zlib 7.4.3 The zlib PHP extension lib-curl 7.68.0 The curl PHP library lib-iconv 2.31 The iconv PHP library lib-libxml 2.9.10 The libxml PHP library lib-openssl 1.1.1.6 OpenSSL 1.1.1f 31 Mar 2020 lib-pcre 10.34 The pcre PHP library lib-xsl 1.1.34 The xsl PHP library php 7.4.3 The PHP interpreter php-64bit 7.4.3 The PHP interpreter, 64bit php-ipv6 7.4.3 The PHP interpreter, with IPv6 support

On which operating system(s) does the issue occur?

Steps to reproduce the issue.

use Kreait\Firebase\Factory; use Kreait\Firebase\Messaging\CloudMessage; use Kreait\Firebase\Messaging\Notification;

$factory = (new Factory)->withServiceAccount('path to sdk json'); $messaging = $factory->createMessaging(); $message = CloudMessage::withTarget('token', 'my device token') ->withNotification(Notification::create('Title', 'Body'));
$messaging->send($message);

Error message/Stack trace

An uncaught Exception was encountered

<p>Type: Kreait\Firebase\Exception\Messaging\MessagingError</p>
<p>Message: syntax error, unexpected '|', expecting variable (T_VARIABLE)</p>
<p>Filename:
    /vendor/kreait/firebase-php/src/Firebase/Exception/MessagingApiExceptionConverter.php
</p>
<p>Line Number: 56</p>
<p>Backtrace:</p>
<p style="margin-left:10px">
    File:
    /vendor/kreait/firebase-php/src/Firebase/Messaging/ApiClient.php<br />
        Line: 44<br />
        Function: convertException          </p>

    <p style="margin-left:10px">
        /vendor/kreait/firebase-php/src/Firebase/Messaging.php<br />
        Line: 54<br />
        Function: send          </p>

Additional information

No response

jeromegamez commented 2 years ago

Thank you for reporting an issue!

The code is tested for Syntax, so a syntax error in the SDK seems unlikely to me šŸ¤”, and if you look at https://github.com/kreait/firebase-php/blob/5.26.1/src/Firebase/Exception/MessagingApiExceptionConverter.php#L56 or https://github.com/kreait/firebase-php/blob/5.26.1/src/Firebase/Messaging/ApiClient.php, there aren't even |s in there šŸ™ˆ

At the moment we can only see where the error was not further handled, but it would be interesting to see where it actually happened. Could you please also share the full stack trace of the error you've encountered?

If you have the means, please provide it as output from the Terminal, not as HTML - HTML is quite difficult to read šŸ˜…

jeromegamez commented 2 years ago

Also, if you're running this in the browser, please make sure that you are using the same PHP version in the Webserver as you're running in the terminal. I've seen reports of errors when it turns out that there were two different PHP versions used.

You should be able to do this by dropping an echo PHP_VERSION; exit somewhere and opening the page in the browser.

Are you aware of https://github.com/tattersoftware/codeigniter4-firebase ? It's a well-maintained Codeigniter integration so that you don't have to handle the Factory yourself (your code looks alright, I just wanted to be sure you know all possible options šŸ¤—)

carinihdev commented 2 years ago

Hi Jeromegamez

Thanks for your fast reponse.

Hmm yes it is weird there is no | in the codes.

So I am using Codeigniter 3.1.11, and use it as an API backend.

For the API, I used chriskacerguis/codeigniter-restserver as well.

The following is the full stack trace. An uncaught Exception was encountered Type: Kreait\Firebase\Exception\Messaging\MessagingError

Message: syntax error, unexpected '|', expecting variable (T_VARIABLE)

Filename: /var/www/development/api/vendor/kreait/firebase-php/src/Firebase/Exception/MessagingApiExceptionConverter.php

Line Number: 56

Backtrace:

File: /var/www/development/api/vendor/kreait/firebase-php/src/Firebase/Messaging/ApiClient.php Line: 44 Function: convertException

File: /var/www/development/api/vendor/kreait/firebase-php/src/Firebase/Messaging.php Line: 54 Function: send

File: /var/www/development/api/application/controllers/Customer.php Line: 3418 Function: send

File: /var/www/development/api/vendor/chriskacerguis/codeigniter-restserver/src/RestController.php Line: 593 Function: call_user_func_array

File: /var/www/development/api/index.php Line: 316 Function: require_once

carinihdev commented 2 years ago

For the testing I m using Postman, by calling to the URL.

Are you aware of https://github.com/tattersoftware/codeigniter4-firebase ? --> Yes. Unfortunately I am still using Codeigniter 3, which is not compatible with Codeigniter 4.

jeromegamez commented 2 years ago

Could you try and and let your Controller output the currently used PHP version? Syntax errors are usually an indicator that the PHP versions on the Terminal and in the Web Server don't match.

carinihdev commented 2 years ago

The test for php version as follow..

From terminal with php -v PHP 7.4.3 (cli) (built: Oct 25 2021 18:20:54) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

From controller code: echo phpversion();

output: 7.4.3

jeromegamez commented 2 years ago

Ok, that's strange! I will try to reproduce the problem in the next days (it's 2:45 a.m. for me right now šŸ˜…), but please let me know if you find a solution earlier.

Another thing you could try is to downgrade the SDK to an earlier version (e.g. 5.14, just to try if it works then). If it does work with an earlier version, try upgrading step by step until it stops working and let me know the version number.

carinihdev commented 2 years ago

Noted will do. Thanks for the support.

carinihdev commented 2 years ago

I have downgraded to "kreait/firebase-php": "5.14", but unfortunately encountered different error below:

An uncaught Exception was encountered Type: Kreait\Firebase\Exception\Messaging\NotFound

Message: Requested entity was not found.

Filename: /var/www/development/api/vendor/kreait/firebase-php/src/Firebase/Exception/Messaging/NotFound.php

Line Number: 22

Backtrace:

File: /var/www/development/api/vendor/kreait/firebase-php/src/Firebase/Exception/MessagingApiExceptionConverter.php Line: 102 Function: withErrors

File: /var/www/development/api/vendor/kreait/firebase-php/src/Firebase/Exception/MessagingApiExceptionConverter.php Line: 108 Function: convertResponse

File: /var/www/development/api/vendor/kreait/firebase-php/src/Firebase/Exception/MessagingApiExceptionConverter.php Line: 50 Function: convertGuzzleRequestException

File: /var/www/development/api/vendor/kreait/firebase-php/src/Firebase/Messaging/ApiClient.php Line: 47 Function: convertException

File: /var/www/development/api/vendor/kreait/firebase-php/src/Firebase/Messaging.php Line: 77 Function: send

File: /var/www/development/api/application/controllers/Customer.php Line: 3416 Function: send

File: /var/www/development/api/vendor/chriskacerguis/codeigniter-restserver/src/RestController.php Line: 593 Function: call_user_func_array

File: /var/www/development/api/index.php Line: 316 Function: require_once

jeromegamez commented 2 years ago

Okay, but that's better - this means that the registration token you're sending the message has been unregistered or doesn't belong to the same Firebase project you're using to send the message.

Could you now upgrade the SDK until you encounter the version where it stops working? I would probably try 5.20, if it works, 5.21, if it doesn't work 5.19 and so on šŸ¤ž

carinihdev commented 2 years ago

The following results are for version 5.20

An uncaught Exception was encountered Type: Kreait\Firebase\Exception\Messaging\NotFound

Message: The message could not be delivered to the device identified by 'xxxxxxxxxxxxxx'. Although the token is syntactically correct, it is not known to the Firebase project you are using. This could have the following reasons: - The token has been unregistered from the project. This can happen when a user has logged out from the application on the given client, or if they have uninstalled or re-installed the application. - The token has been registered to a different Firebase project than the project you are using to send the message. A common reason for this is when you work with different application environments and are sending a message from one environment to a device in another environment.

Filename: /var/www/development/api/vendor/kreait/firebase-php/src/Firebase/Exception/Messaging/NotFound.php

Line Number: 40

Backtrace:

File: /var/www/development/api/vendor/kreait/firebase-php/src/Firebase/Messaging.php Line: 58 Function: becauseTokenNotFound

File: /var/www/development/api/application/controllers/Customer.php Line: 3416 Function: send

File: /var/www/development/api/vendor/chriskacerguis/codeigniter-restserver/src/RestController.php Line: 593 Function: call_user_func_array

File: /var/www/development/api/index.php Line: 316 Function: require_once

carinihdev commented 2 years ago

Version 5.19, 5.20 has the same result with 5.21. --> Message: The message could not be delivered to the device identified...

carinihdev commented 2 years ago

5.25 and 5.26 has the same result as well --> Message: The message could not be delivered to the device identified...

strangely, now 5.26.1 has the same result now -->

An uncaught Exception was encountered Type: Kreait\Firebase\Exception\Messaging\NotFound

Message: The message could not be delivered to the device identified by 'xxxxxxxxxxxxx'. Although the token is syntactically correct, it is not known to the Firebase project you are using. This could have the following reasons: - The token has been unregistered from the project. This can happen when a user has logged out from the application on the given client, or if they have uninstalled or re-installed the application. - The token has been registered to a different Firebase project than the project you are using to send the message. A common reason for this is when you work with different application environments and are sending a message from one environment to a device in another environment.

Filename: /var/www/development/api/vendor/kreait/firebase-php/src/Firebase/Exception/Messaging/NotFound.php

Line Number: 40

Backtrace:

File: /var/www/development/api/vendor/kreait/firebase-php/src/Firebase/Messaging.php Line: 58 Function: becauseTokenNotFound

File: /var/www/development/api/application/controllers/Customer.php Line: 3416 Function: send

File: /var/www/development/api/vendor/chriskacerguis/codeigniter-restserver/src/RestController.php Line: 593 Function: call_user_func_array

File: /var/www/development/api/index.php Line: 316 Function: require_once

jeromegamez commented 2 years ago

That's good! All these messages say that the target device couldn't be reached, in the latest version even with an example.

Since you now don't receive a syntax error anymore, I have to assume that something went wrong during the composer install and perhaps left some files corrupted, or that a file has been altered somehow and introduced the syntax error.

I would consider this issue closed then šŸ˜…, if you agree šŸ¤ž

carinihdev commented 2 years ago

Yes issue closed. Thanks very much !

eramudeep commented 1 year ago

Still facing this issue : php: 8 "kreait/firebase-php": "^7.2.1", "google/cloud": "^0.203.2", "grpc/grpc": "^1.38"