PHP code example of unopim / api-php-client
1. Go to this page and download the library: Download unopim/api-php-client library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
unopim / api-php-client example snippets
use Unopim\ApiClient\UnoPimClient;
$client = UnoPimClient::create(
baseUrl: 'https://your-unopim.example.com',
clientId: 'your-client-id',
clientSecret: 'your-client-secret',
username: '[email protected] ',
password: 'your-admin-password',
);
foreach ($client->products()->iterate() as $product) {
echo $product['sku'], PHP_EOL;
}
$product = $client->products()->get('MY-SKU-001');
$client->products()->create([
'sku' => 'NEW-SKU',
'parent' => null,
'family' => 'default',
'type' => 'simple',
'additional' => null,
'values' => [
'common' => [
'sku' => 'NEW-SKU',
'name' => 'My Product',
],
'channel_locale_specific' => [
'ecommerce' => [
'en_US' => [
'description' => '<p>Hello world.</p>',
'price' => '{"USD":"49.99"}',
],
],
],
],
]);
$client->products()->update('NEW-SKU', [
'sku' => 'NEW-SKU',
'family' => 'default',
'type' => 'simple',
'values' => [
'common' => ['sku' => 'NEW-SKU', 'name' => 'Updated Name'],
],
]);
$client->products()->delete('NEW-SKU');
// All categories
$categories = $client->categories()->list();
// Create attribute
$client->attributes()->create([
'code' => 'color',
'type' => 'select',
'labels' => ['en_US' => 'Color'],
'is_unique' => false,
'is_
$client->mediaFiles()->uploadProductMedia('/path/to/image.jpg');
$client->get('/api/v1/rest/some-endpoint');
$client->post('/api/v1/rest/some-endpoint', $payload);
$client->put('/api/v1/rest/some-endpoint/code', $payload);
$client->delete('/api/v1/rest/some-endpoint/code');
// Auto-paginates
$all = $client->fetchAll('/api/v1/rest/products', batchSize: 100);
use Unopim\ApiClient\Exception\ApiException;
use Unopim\ApiClient\Exception\AuthenticationException;
try {
$client->products()->get('MISSING-SKU');
} catch (AuthenticationException $e) {
// 401 — bad credentials
} catch (ApiException $e) {
$e->getStatusCode(); // int
$e->getResponseBody(); // string
$e->getMessage();
}
use GuzzleHttp\Client as Guzzle;
use GuzzleHttp\Psr7\HttpFactory;
use Unopim\ApiClient\UnoPimClient;
$g = new Guzzle(['timeout' => 30]);
$f = new HttpFactory();
$client = UnoPimClient::createWithHttpClient(
'https://your-unopim.example.com', 'cid', 'csec', '[email protected] ', 'pass',
$g, $f, $f,
);
use Symfony\Component\HttpClient\Psr18Client;
use Unopim\ApiClient\UnoPimClient;
$s = new Psr18Client();
$client = UnoPimClient::createWithHttpClient(
'https://your-unopim.example.com', 'cid', 'csec', '[email protected] ', 'pass',
$s, $s, $s,
);
bash
composer