Данная документация описывает библиотеки для работы с API
// PHP usage include('ZCHBapi.php'); // Создаем экземпляр (инстанс) класса ZCHBapi $zchb = new ZCHBapi; // Задаём свой API ключ $zchb->setApikey('Ваш API ключ'); // Задаём массив с параметрами $arr = [ 'id' => '1063667299850', 'page' => '1', ]; // Вызываем метод запроса с аргументами: [имя метода API], [массив с параметрами] // Получаем ответ $response = $zchb->request('card', $arr);
/** * Class for simple use API ZACHESTNYIBIZNES with PHP * Documentation available at https://zachestnyibiznesapi.ru */ class ZCHBapi { /** * Base API URL for module "paid" */ const BASE_URL = 'https://zachestnyibiznesapi.ru/paid/data/'; /** * User's api key */ private $apikey; /** * Set api key for further use * @param string $apiKey api key from user */ public function setApikey($apikey) { $this->apikey = $apikey; } /** * Request to API URL * 1) Set context for request * 2) Decode answer(JSON) to array * @param string $method method from API documentation * @param array $query user's parameters * @return array answer from API */ public function request($method, $query) { $context = stream_context_create($this->getParams($query)); return json_decode(file_get_contents(self::BASE_URL.$method, false, $context), true); } /** * Bringing the parameters to the query string * 1) Set base parameters for POST request's context * 2) Add user's with needle parameters to POST request's context * @param array $query user's parameters * @return array $context correct array of parameters for POST request's context */ public function getParams($query) { $context = [ 'http' => [ 'method' => 'POST', 'header' => 'Content-Type: application/x-www-form-urlencoded', ], ]; $context['http']['content'] = http_build_query(array_merge($query, [ 'api_key' => $this->apikey, '_format' => 'json', ])); return $context; }
//Подключаем Файл JavaScript <script type="text/javascript" src="ZCHBapi.js"></script> <script> // Создаем экземпляр (инстанс) класса zchb = new ZCHBapi; // Задаём свой API ключ zchb.setApikey('Ваш API ключ'); // Задаём массив с параметрами в формате JSON jsonArray = {'id':'1063667299850','page':'1'}; // Вызываем метод запроса с аргументами: [имя метода API], [массив с параметрами], [id элемента HTML для отображения ответа] // Получаем ответ zchb.request('card', jsonArray, 'anotherID'); </script> //Элемент HTML для отображения ответа <p id="anotherID"></p>
/** * Class for simple use API ZACHESTNYIBIZNES with JS * Documentation available at https://zachestnyibiznesapi.ru */ 'use strict' class ZCHBapi { /** * Base API URL for module "paid" * Base API format for module "paid" */ constructor() { this.baseUrl = 'https://zachestnyibiznesapi.ru/paid/data/' this.format = 'json' } /** * Set api key for further use * @param apikey */ setApikey(apikey) { this.apiKey = apikey } /** * Request to API URL * 1) Set context for request * 2) Prepare answer(Array) to string for POST * @param method * @param query * @param id view id for render HTML element * @return array answer from API */ request(method, query, id = false) { query = this.getParams(query); const xhr = new XMLHttpRequest(); xhr.open('POST', this.baseUrl + method + '?api_key=' + this.apiKey , true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.onreadystatechange = function() { if (this.readyState !== 4) return; const obj = JSON.parse(this.responseText); document.getElementById(id).innerHTML = ZCHBapi.print_r(obj); if (this.status !== 200) console.log( 'ошибка: ' + (this.status ? this.statusText : 'запрос не удался')); } xhr.send(query); } /** * Bringing the parameters to the query string * 1) Set base parameters for POST request's context * 2) Add user's with needle parameters to POST request's context * @return array context correct array of parameters for POST request's context * @param query */ getParams(query) { let string = ''; let i = 0; query['api_key'] = this.apiKey; for (let key in query ) { if(typeof query[key] === 'object' || typeof query[key] === 'array') { for (let key1 in query[key]) { for (let key2 in query[key]) { string += key +'['+key1+']' + '[]=' + query[key][key1][key2] + '&'; } i++; } } else string += key + '=' + query[key] + '&'; } return string; } /** * Prepare answer for render to HTML element * 1) Set answer array/object * 2) Level Tabulation into HTML element * @return array context correct answer view for HTML element * @param arr * @param level */ static print_r(arr, level = 0) { let print_red_text = ""; if (!level) level = 0; let level_padding = ""; for (let j = 0; j < level + 1; j++) level_padding += " "; if (typeof(arr) == 'object') { for (const item in arr) { const value = arr[item]; if (typeof(value) == 'object') { print_red_text += level_padding + "'" + item + "' :\n"; print_red_text += ZCHBapi.print_r(value, level + 1); } else print_red_text += level_padding + "'" + item + "' => \"" + value + "\"\n"; } } else print_red_text = "===>" + arr + "<===(" + typeof(arr) + ")"; return print_red_text; } }