Created
April 23, 2017 10:44
-
-
Save nepda/84d6d68fa92bb96457153b6304097258 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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