Dodaje listę atrybutów
array call(string $session_id, "attribute.list.create", array($list))
<?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) { $attribute1 = Array( "pres_id" => 1, "name" => "Test dodania atrybutu 1", "description" => "Opis atrybutu 1", "order" => 1, "type" => 2, // typ select "active" => 1, "default" => "Wartość 2", "options" => Array( "Wartość 1", "Wartość 2", "Wartość 3", ), ); $attribute2 = Array( "pres_id" => 1, "name" => "Test dodania atrybutu 2", "description" => "Opis atrybutu 2", "order" => 1, "type" => 2, // typ select "active" => 1, "default" => "Wartość 2", "options" => Array( "Wartość 1", "Wartość 2", "Wartość 3", ), ); $attributes = Array( $attribute1, $attribute2 ); $params = Array( "method" => "call", "params" => Array($session, "attribute.list.create", Array($attributes)) ); // 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 { foreach ($result as $r) { if ($r == 0) { echo "Operacja się nie udała" . "<br>"; } else if ($r == -1) { echo "Podane dane są nieprawidłowe i nie spełniają wymagań walidacji" . "<br>"; } else if ($r == -2) { echo "Operacja się nie udała - zabrakło czasu na zrealizowanie operacji" . "<br>"; } else { echo "Cecha została dodana, id: " . $r . "<br>"; } } } } else { echo "Wystąpił błąd logowania"; } curl_close($c); ?>