Search code examples
google-cloud-messaginggoogle-cloud-platformgoogle-app-engine-php

Google Cloud PubSub with PHP - how to install classes on the App Engine server side?


I'm trying to use the Google messaging system but I'm failing before starting. I'm trying to start with a super simple app that only transmit a "hello world" message and then capture it. Saving it in DB and other tasks will be done after this service is up and running.

However I can't get it to work.I get the error:

[my-app].appspot.com is currently unable to handle this request. HTTP ERROR 500

On logs, I see the problem:

PHP Fatal error: Class 'Google\Cloud\PubSub\PubSubClient' not found in /base/data/home/apps/g~[my-app]/20170809t182312.403268501910529191/printHello.php on line 14

Line 14 is: $pubsub = new PubSubClient([ which is copy paste from google documentation

Consider:

  • The app is working fine without the: use Google\Cloud\PubSub\PubSubClient;. I delete everything except an echo "... some text praying to make it work..." and it is displayed. So no problem in app.yaml file or somewhere else.

  • I was able to send a message from the topic detail page and receive it in web-console so all authorizations and enabling are done correct.

  • on local machine, Intellij IDEA recognized the PubSubClient class after I installed beta components. The app is not working on local machine, but I did not even try it. I want to be able to make it work on the server first time.

  • I checked and all files from local are present on the server as well.

What am I missing? I read all pages I found on cloud.google. ... and all I found on the internet but it seems nobody had a similar problem. So it must be something very simple that I'm doing wrong or have not done. Any suggestions?


Solution

  • Google Cloud PubSub is not by default enabled in GAE. You need to add it to your app code. From Pure PHP:

    You can include other pure PHP libraries with your application by putting the code in your application's directory, which is the same directory that contains your app.yaml file.

    For example, you can create a symbolic link in your application's directory that points to a library's directory. That link is then followed and that library gets included in your application when you deploy to App Engine.

    You can also include PHP libraries by specifying php.ini directives and including PHP include statements in your code. However, the preferred alternative is to use a PHP dependency management tool such as Composer.

    I'd check in Intellij IDEA where is the library/class located on your local system and symlink it into your app dir. It might be necessary to adjust the use statement and/or the symlink source dir to get things going.

    Note: I'm a Python, not a PHP user, so take this with a grain of salt.