PHP API v2.0 issue with 'use Patreon/API'

So, I have been looking around for a while, and I know this is just a hick-up, but it won’t go away, so here is a quick overview of my setup, and the issue I am stuck on. If anyone has experienced this, and could answer, that would be great.

So, I had api v1 set up and it works perfectly. Sign up and sign in are working. But, I want to include some creator tier access privacy on “my” multi-user blogging platform, so I want to setup api v2 to access user’s patrons, and allow access to tiered content on the blog platform.

So, I installed COMPOSER, to a limited access level /var/www/mysite.com/assets/php/_api/composer.phar
in an nginx server btw ( not that that matters ).

Then I installed the Patreon API v2.
composer require patreon/patreon

All good so far.
Then I setup a little test environment to walk through a conversion of api v1 to api v2.
Here is the current issue I am stuck on
This test file looks like this so far:

var_dump(class_exists('Patreon\API'));
require_once( $_SERVER['DOCUMENT_ROOT'] . '/assets/php/_api/vendor/autoload.php');
var_dump(class_exists('Patreon\API'));

$Api = Patreon\API;
//use Patreon\API;
//use Patreon\OAuth;

I commented out the “use Patreon.…” statements as that was giving a complete output break with no error data.

Essentially the namespace exists, as I can see from the output:

…assets/php/_requests/test.php:1:boolean: false
…assets/php/_requests/test.php:3:boolean: true

But I get these errors:

Fatal error: Uncaught Error: Undefined constant ‘Patreon\API’ in …assets/php/_requests/test.php on line 5

Error: Undefined constant ‘Patreon\API’ in
…assets/php/_requests/test.php on line 5

I have also stepped through a bit of the autoload.php and the autoload_real.php files to ensure that the class was being instantiated.

A guess:

  1. I am new to namespaces, but there is no require of API.php nor OAuth.php in the vender/ folder files, which seems odd to me, maybe it only did a partial install?

  2. Something else.

Any help on the topic would be much appreciated. Maybe I can send someone a coffee, or 17 of them.

I commented out the “use Patreon…” statements as that was giving a complete output break with no error data.

This would cause the classes to be loaded incorrectly. The namespaces must be there. Also, Initialization of the $Api object over Patreon\API class must come after the namespace declarations.

1 Like

Thank you very much. Like I said some of this is new to me, namely composer and namespaces, and I was making an assumption that was the source of the error.

For future readers: after testing:

require_once( $pathTo . '/_api/vendor/autoload.php');
use Patreon\API;
use Patreon\OAuth;

I got this error:

parse error: syntax error, unexpected ‘use’ (T_USE)

What I learned is that you cannot instantiate a namespace inside of a method.
So, I dropped the Class encapsulation, and it works fine.

Now the fun begins.
Lot’s to look at.
Thanks muchly to codebard.

1 Like

Right, namespace declarations need to be at the top of your files. Glad to hear you got it working. Good luck with your app.

1 Like