Skip to main content
How-to

PHP SDK

Install the PHP SDK with Composer, authenticate, and retrieve an order.

Before you start

  • A Simple Key. Generate one under Configuration → Back Office → Authorized Applications. See Creating a Simple Key. For an application that multiple merchants connect to their own accounts, use OAuth 2.0 instead.
  • PHP 7.4 or later. The SDK also works on PHP 8.

Install

Add the package to composer.json:

{
"require": {
"ultracart/rest_api_v2_sdk_php": "4.1.129"
}
}

Then install and include the autoloader:

composer install
<?php
require_once 'vendor/autoload.php';

Check Packagist for the current release before pinning a different version.

Authenticate

Every API class has a static usingApiKey() factory that builds a configured client in one call:

<?php
require_once 'vendor/autoload.php';

use ultracart\v2\api\OrderApi;

$order_api = OrderApi::usingApiKey(getenv('UC_API_KEY')); // <- your merchant Simple Key

usingApiKey() sets the X-UltraCart-Api-Version header along with the credential, so no further client setup is needed. It also accepts optional arguments for retry seconds, TLS verification, and debug output. Leave TLS verification at its default of true.

warning

Read the key from the environment rather than hardcoding it. The samples repository hardcodes a shared development key and sets VERIFY_SSL = false; neither belongs in your code.

Retrieve an order

This is the getOrder sample from sdk_samples, trimmed to the call itself:

<?php
// Trimmed from sdk_samples/php/order/getOrder.php

$expansion = "item,summary,billing,shipping,shipping.tracking_number_details";
$order_id = 'DEMO-0009104390'; // <- an order ID in your account

$api_response = $order_api->getOrder($order_id, $expansion);

if ($api_response->getError() != null) {
error_log($api_response->getError()->getDeveloperMessage());
error_log($api_response->getError()->getUserMessage());
exit();
}

$order = $api_response->getOrder();
var_dump($order);

The expansion string controls how much of the order comes back. Order objects are large, and requesting every branch across thousands of orders is the most common cause of slow SDK code. Expanding objects lists the valid values.

UltraCart application errors arrive on the response through getError(), with a developer message and a user message, rather than as a thrown exception.

Next

Was this page helpful?