Pobiera listę producentów
array call(string $session_id, "producer.list", array($extended = false, $producers = ""))
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, "producer.list", Array(true, true, true, true, true, Array(5, 6, 7) // id producentó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) { $producer = (Array)$item; echo "Id: " . $producer['producer_id'] . "<br>"; echo "Nazwa: " . $producer['name'] . "<br>"; echo "<hr><br>"; } } } else { echo "Wystąpił błąd logowania"; } curl_close($c); ?>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using System.Web.Script.Serialization; namespace producer.list { class Program { public static Object FromJson(string input) { JavaScriptSerializer serializer = new JavaScriptSerializer(); return serializer.Deserialize<Object>(input); } public static string ToJson(object input) { JavaScriptSerializer serializer = new JavaScriptSerializer(); return serializer.Serialize(input); } public static Object sendApiRequest(String method, Object[] methodParams) { WebRequest request = WebRequest.Create("http://shop.example.com/webapi/json/"); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; Stream dataStream = request.GetRequestStream(); Dictionary<String, Object> postParams = new Dictionary<String, Object>(); postParams.Add("method", method); postParams.Add("params", methodParams); string jsonEncodedParams = ToJson(postParams); System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); byte[] byteArray = encoding.GetBytes("json=" + jsonEncodedParams); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse webResponse = request.GetResponse(); Stream responseStream = webResponse.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); string text = reader.ReadToEnd(); Object response = FromJson(text); return response; } public static String login(String login, String password) { Object[] methodParams = { login, password }; Object response = sendApiRequest("login", methodParams); String session = null; if (response is Dictionary<String, Object>) { Dictionary<String, Object> d = (Dictionary<String, Object>)response; if (d.ContainsKey("error")) { Console.WriteLine("Wystąpił błąd: {0}, kod: {1}", d["error"], d["code"]); } } else if (response is String) { session = (String)response; } return session; } static void Main(string[] args) { String session = login("api", "test"); if (session != null) { Object[] methodParams = { session, "producer.list", new Object[] { true, null } }; Object response = sendApiRequest("call", methodParams); if (response is Dictionary<String, Object>) { Dictionary<String, Object> d = (Dictionary<String, Object>)response; if (d.ContainsKey("error")) { Console.WriteLine("Wystąpił błąd: {0}, kod: {1}", d["error"], d["code"]); } } else if (response is Object[]) { foreach (Object obj in (Object[])response) { Dictionary<String, Object> d = (Dictionary<String, Object>)obj; Console.WriteLine("Id producenta: " + d["producer_id"]); Console.WriteLine("Nazwa producenta: " + d["name"]); // itp. Console.WriteLine(); } } } else { Console.WriteLine("Wystąpił błąd logowania"); } Console.ReadKey(); } } }