Pobiera listę produktów
array call(string $session_id, "product.list", array($extended = false, $translations = false, $options = false, $gfx = false, $attributes = false, $products = ""))
W celu zapewnienia kompatybilności typów zwracanych obiektów kod błędu jest sygnalizowany przez tablicę asocjacyjną zawierającą klucz 'error'
<?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) { $params = Array( "method" => "call", "params" => Array($session, "product.list", Array(true, true, true, true, true, Array(59, 91, 92, 93, 94) // id produktów ) ) ); // 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 $item) { $product = (Array)$item; echo "Id: " . $product['product_id'] . "<br>"; $translations = (Array)$product['translations']; $translPL = (Array)$translations['pl_PL']; echo "Nazwa w języku polskim: " . $translPL['name'] . "<br>"; $stock = (Array)$product['stock']; echo "Cena: " . $stock['price'] . "<br>"; echo "Stan magazynowy: " . $stock['stock'] . "<br>"; $gfx = (Array)$product['images']; foreach ($gfx as $g) { $image = (Array)$g; echo "Id zdjęcia: " . $image['gfx_id'] . "<br>"; } $attributes = (Array)$product['attributes']; foreach ($attributes as $attGId => $attG) { echo "Id grupy atrybutów: " . $attGId . "<br>"; $attGroup = (Array)$attG; foreach ($attGroup as $attVId => $attV) { echo " - id atrybutu: " . $attVId . ", wartość: " . $attV . "<br>"; } } echo "<hr><br>"; } } } else { echo "Wystąpił błąd logowania"; } curl_close($c); ?>