#api #get #postman #prestashop #put
#API #получить #postman #prestashop #положить
Вопрос:
Это мой вызов GET API в POSTMAN
{
"stock_available": {
"id": 271,
"id_product": "231",
"id_product_attribute": "0",
"id_shop": "1",
"id_shop_group": "0",
"quantity": "0",
"depends_on_stock": "0",
"out_of_stock": "0",
"location": ""
}
}
Здесь я преобразую ответ в переменную
const response = pm.response.json();
pm.globals.set("quantity", response.stock_available.quantity);
Это вызов PUT, который я хочу сделать
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<stock_available>
<id>1445</id>
<id_product>1406</id_product>
<id_product_attribute>0</id_product_attribute>
<id_shop>1</id_shop>
<id_shop_group>0</id_shop_group>
<quantity>{{quantity}}</quantity>
<depends_on_stock>0</depends_on_stock>
<out_of_stock>2</out_of_stock>
<location></location>
</stock_available>
</prestashop>
Ответ №1:
Мое решение:
<?php
define('DEBUG', false);
ini_set('display_errors','on');
define('PS_SHOP_PATH', '*your url here*');
define('PS_WS_AUTH_KEY', '*your api key here*');
try {
$webService= new PrestaShopWebservice(PS_SHOP_PATH, PS_WS_AUTH_KEY, DEBUG);
$xml = $webService->get([
'resource' => 'stock_availables',
'id' => 231,
]);
$quantity = $xml->stock_available->quantity;
$xml = $webService->get([
'resource' => 'stock_availables',
'id' => 1406,
]);
$xml->stock_available->quantity = $quantity;
$opt['putXml'] = $xml->asXML();
$opt['id'] = 1406;
$opt['resource'] = 'stock_availables';
$xml = $webService->edit($opt);
}catch (PrestaShopWebserviceException $ex) {
echo "Error:<br>";
echo $ex->getMessage();
exit(1);
}