PHP Initialization for Square

I’ve seen a few code examples through search engines, but they don’t seem to match up with the api explorer.

I installed the square php kit using composer.

How do I initialize it using my app token? It would be nice if the Squre API Explorer provided ALL code much like facebooks API explorer, rather than just the post initialized code.

Sandbox example (change environment to PRODUCTION for production):

$client = new SquareClient([
    'accessToken' => 'YOUR SANDBOX ACCESS TOKEN HERE',
    'environment' => Environment::SANDBOX,
]);

I’ll share your feedback about this with the API Explorer team. The above can be found in the README of the SDK.

ya, thats the one I’ve tried.

But I get this error;

'Uncaught Error: Class ‘SquareClient’ not found ’

When using the composer to install, do I need to modify any files to get it included?

In my netbeans, if I type New Sq, it suggests ‘Sqaure’ but not ‘SquareClient’ that leads me to believe that it is included, but not sure…

Sorry for not explaining clearly; you do need to also include it at the top:

require 'vendor/autoload.php'; // this should have been created by Composer

use Square\SquareClient;
1 Like

Uncaught Error: Class ‘Environment’

include ‘includes.php’;

use Square\SquareClient;

$client = new SquareClient([
‘accessToken’ => getSiteMeta(“SQ_APP_TOKEN”),
‘environment’ => Environment::PRODUCTION,
]);

$body = new \Square\Models\CreateCustomerRequest();
$body->setEmailAddress(‘[email protected]’);

$api_response = $client->getCustomersApi()->createCustomer($body);

if ($api_response->isSuccess()) {
$result = $api_response->getResult();
} else {
$errors = $api_response->getErrors();
}

my autoload is within the includes.php file.

I am getting an error for the environment now.

Oh shoot, I left out a line sorry about that:

use Square\Environment;

Pretty much anything within Square that you use, you’ll need to add a line near the top to make it available (use xyz).

I think you guys should add this information to the API explorer so that anyone testing can get the full picture of what code is actually being executed. Would save a lot of headache.

2 Likes