Skip to content

Instantly share code, notes, and snippets.

@nepda
Created April 23, 2017 10:44
Show Gist options
  • Save nepda/84d6d68fa92bb96457153b6304097258 to your computer and use it in GitHub Desktop.
Save nepda/84d6d68fa92bb96457153b6304097258 to your computer and use it in GitHub Desktop.
<?php
require_once 'vendor/autoload.php';
$integrationAdminTokenService = new Swagger\Client\Api\IntegrationAdminTokenServiceV1Api();
$loginBody = new \Swagger\Client\Model\Body136();
$loginBody->setPassword('username');
$loginBody->setUsername('password');
$token = $integrationAdminTokenService->integrationAdminTokenServiceV1CreateAdminAccessTokenPost($loginBody);
$token = substr($token, 1, -1); // remove quotes
echo sprintf('Token fetched: "%s".%s', $token, PHP_EOL);
$config = new \Swagger\Client\Configuration();
// Don't know if this is the correct way but it works
$config->addDefaultHeader('Authorization', 'Bearer ' . $token);
$api = new \Swagger\Client\ApiClient($config);
$productRepoService = new \Swagger\Client\Api\CatalogProductRepositoryV1Api($api);
try {
$customAttribute = new \Swagger\Client\Model\FrameworkAttributeInterface;
$customAttribute->setAttributeCode('my_attribute_134')
->setValue('test value');
$customAttributes = [$customAttribute];
$product = new \Swagger\Client\Model\CatalogDataProductInterface();
$product->setSku('TEST-001')
->setTypeId(4)
->setName('my product name')
->setCustomAttributes($customAttributes)
->setAttributeSetId(4);
if ($product->valid()) {
$createProduct = new \Swagger\Client\Model\Body18();
$createProduct->setProduct($product);
if ($createProduct->valid()) {
$result = $productRepoService->catalogProductRepositoryV1SavePost($createProduct);
print_r($result);
} else {
echo 'createProduct is not valid (local check)' . PHP_EOL;
echo implode(PHP_EOL, $createProduct->listInvalidProperties()) . PHP_EOL;
}
} else {
echo 'product is not valid (local check)' . PHP_EOL;
echo implode(PHP_EOL, $product->listInvalidProperties()) . PHP_EOL;
}
} catch (\Swagger\Client\ApiException $apiException) {
echo 'Exception: ' . PHP_EOL;
echo print_r($apiException->getResponseBody());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment