Hello,
There is a mention in this article as a link to the example script. I provide a copy of the link below in this reply.
You must configure some variables in the script using the Google OAuth application to access Gmail API. There is a description in the article for the steps to get the OAuth application values.
Can you try the example script and let me know if it works well for you?
phpclasses.org/package/9-PHP-PHP-ma
...