Dodaje produkt
array call(string $session_id, "product.create", array($data))
<?php /** * Logowanie do API * * @param resource $c cURL resource handle * @param string $login Login użytkownika * @param string $password Hasło użytkownika * @return string Indentyfikatorr sesji użytkownika */ function login($c, $login, $password) { $params = Array( "method" => "login", "params" => Array($login, $password) ); curl_setopt($c, CURLOPT_POSTFIELDS, "json=" . json_encode($params)); $result = (Array) json_decode(curl_exec($c)); if (isset($result['error'])) { return null; } else { return $result[0]; } } $c = curl_init(); curl_setopt($c, CURLOPT_URL, 'http://shop.example.com/webapi/json/'); curl_setopt($c, CURLOPT_POST, true); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); // zalogowanie użytkownika i pobranie identyfikatora sesji $session = login($c, "api", "test"); if ($session != null) { $product = Array( "producer_id" => null, "tax_id" => 1, "category_id" => 9, "unit_id" => 2, "other_price" => 9.50, "code" => md5(rand()), "pkwiu" => null, "stock" => Array( "price" => 8.99, "stock" => 10, "warn_level" => 2, "sold" => 0, "weight" => 1.2, "availability_id" => null, "delivery_id" => null, "gfx_id" => null, ), "translations" => Array( "pl_PL" => Array( "name" => "Produkt testowy", "short_description" => "Krótki opis", "description" => "Opis produktu testowego", "active" => 1, "seo_title" => "Produkt testowy", "seo_description" => "Opis produktu testowego", "seo_keywords" => "produkt, test, api", "order" => null, "main_page" => 0, "main_page_order" => null, ), ), "options" => Array( // warianty Array( "price" => 1.50, "price_type" => 2, // dodanie ceny "stock" => 9, "warn_level" => 2, "weight" => 1.3, "weight_type" => 1, // nowa waga "active" => 1, "default" => 0, "availability_id" => null, "delivery_id" => null, "gfx_id" => null, "options" => Array( // id_wariantu => id_wartości 1 => 5, ), ), Array( "price" => 1.55, "price_type" => 2, // dodanie ceny "stock" => 8, "warn_level" => 3, "weight" => 1.4, "weight_type" => 1, // nowa waga "active" => 1, "default" => 0, "availability_id" => null, "delivery_id" => null, "gfx_id" => null, "options" => Array( // id_wariantu => id_wartości 1 => 6, ), ), // itp. ), ); $params = Array( "method" => "call", "params" => Array($session, "product.create", Array($product)) ); // zakodowanie parametrów dla metody POST $postParams = "json=" . json_encode($params); curl_setopt($c, CURLOPT_POSTFIELDS, $postParams); // dekodowanie rezultatu w formacie JSON do tablicy result $data = curl_exec($c); $result = (Array)json_decode($data); // sprawdzenie, czy wystąpił błąd if (isset($result['error'])) { echo "Wystąpił błąd: " . $result['error'] . ", kod: " . $result['code']; } else { if ($result[0] == 0) { echo "Operacja się nie udała"; } else if ($result[0] == -1) { echo "Podane dane są nieprawidłowe i nie spełniają wymagań walidacji"; } else { echo "Produkt został dodany, id: " . $result[0]; } } } else { echo "Wystąpił błąd logowania"; } curl_close($c); ?>