Creating a Customer in PHP

Using Square's Customers API using PHP

Reddit
LinkedIn

You can’t have a business if you don’t have any customers, but importing customers from an existing system can be a pain, and sometimes you need to be able to do so programmatically. In PHP, our PHP SDK makes it fairly easy to create these customers using one simple script:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken('XXXX');


$customers_api = new SquareConnect\Api\CustomersApi();
$customer = new \SquareConnect\Model\CreateCustomerRequest();

$customer->setGivenName('Stacy');
$customer->setFamilyName('Smith');
$customer->setEmailAddress('stacy.s@example.com');
$customer->setAddress(
    array(
        'address_line_1'=>'123 Main St.',
        'locality'=>'Anywhere',
        'country'=>'US'
    )
);

try {
    $result = $customers_api->createCustomer($customer);
    print_r($result);
} catch (Exception $e) {
    echo 'Exception when calling CustomersApi->createCustomer: ', $e->getMessage(), PHP_EOL;
}
?>

The code starts out by loading the PHP SDK that was installed with Composer and then setting the access token for the account. Then you can create a new instance of the CustomersApi and a new object that will hold all of the customer information that you are trying to upload. Finally, you call the createCustomer() method with your customer object and voilà, you have a brand new customer in your account.

I hope you liked this post, let me know what you’d like the next one to be about by reaching out to @SquareDev on twitter.