Сама таблица для СДЭК городов в MySQL CREATE TABLE IF NOT EXISTS `cdek_city` ( `cityName` varchar(199) NOT NULL, `cityCode` int(11) NOT NULL, `cityUuid` varchar(199) NOT NULL, `country` varchar(199) NOT NULL, `countryCode` varchar(10) NOT NULL, `region` varchar(199) NOT NULL, `regionCode` int(11) NOT NULL, `regionCodeExt` int(11) NOT NULL, `subRegion` varchar(199) NOT NULL, `latitude` varchar(50) NOT NULL, `longitude` int(50) NOT NULL, `kladr` varchar(50) NOT NULL, `fiasGuid` varchar(199) NOT NULL, `paymentLimit` varchar(199) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8; Скрипт для заливания данных в таблицу с JSON API СДЭК getConnection(); for ($i=1;$i<=45;$i++) { $a = json_decode(file_get_contents("http://integration.cdek.ru/v1/location/cities/json?size=1000&page=$i")); foreach ($a as $v) { $q = ""; $q .= "INSERT INTO `cdek_city` ("; $iii=-1; foreach ($v as $kk => $vv) { $iii++; if ($iii != 0) {$q .= ",";} $q .= "`$kk`"; } $q .= ") VALUES ("; $iii=-1; foreach ($v as $kk => $vv) { $iii++; if ($iii != 0) {$q .= ",";} $q .= "'$vv'"; } $q .= ");\n"; //echo "$q\n"; $result = $connection->query($q); } echo "($i)"; } echo "ok"; Серверная часть для Ajax возвращающая список названий городов и id по началу имени status = 1; $JSONResults->msg = "Возвращён список городов"; global $DB; $text = $DB->ForSql($_REQUEST["text"]); $connection = Main\Application::getInstance()->getConnection(); $result = $connection->query("SELECT * FROM `cdek_city` WHERE `cityName` LIKE '$text%';"); $iii = -1; foreach ($result->fetchAll() as $v) { $iii++; $id = $v["cityCode"]; $name = $v["cityName"]; if ($v["region"] and $v["subRegion"]) { $name .= " ["; if ($v["region"]) { $name .= $v["region"]; } if ($v["subRegion"]) { if ($v["region"]) { $name .= "/"; } $name .= $v["subRegion"]; } $name .= "]"; } if ($iii < 15) { $JSONResults->items[] = array("id"=>$id,"name"=>$name); } } } echo json_encode($JSONResults); Серверная часть для AJAX - возвращает список городов по началу названия или считает стоимость доставки(в зависимости от параметра action) status = 1; $JSONResults->msg = "Возвращён список городов"; global $DB; $text = $DB->ForSql($_REQUEST["text"]); $connection = Main\Application::getInstance()->getConnection(); $result = $connection->query("SELECT * FROM `cdek_city` WHERE `cityName` LIKE '$text%';"); $iii = -1; foreach ($result->fetchAll() as $v) { $iii++; $id = $v["cityCode"]; $name = $v["cityName"]; if ($v["region"] and $v["subRegion"]) { $name .= " ["; if ($v["region"]) { $name .= $v["region"]; } if ($v["subRegion"]) { if ($v["region"]) { $name .= "/"; } $name .= $v["subRegion"]; } $name .= "]"; } if ($iii < 15) { $JSONResults->items[] = array("id"=>$id,"name"=>$name); } } } } elseif ($_REQUEST["action"] == "calculate") { // Подключаем библиотеку для отправки запросов по curl require_once('curlPro.php'); $cp = new curlPro; $cp->newip("api.cdek.ru"); $cp->newuri("/calculator/calculate_price_by_json.php"); class forRequest { public $version = "1.0"; public $dateExecute = ""; public $senderCityId = "164"; public $receiverCityId = "0"; public $tariffId = "1"; public $goods = array( array( "weight"=>"5", "length"=>"40", "width"=>"50", "height"=>"60" ), ); } $forRequest = new forRequest; $forRequest->dateExecute = date("Y-m-d",time()); if (isset($_REQUEST["id"]) and ($_REQUEST["id"] != "")) { $forRequest->receiverCityId = $_REQUEST["id"]; } $cp->postactivedata = json_encode($forRequest); $text = $cp->go(); if ($oj = json_decode($text->val())) { $JSONResults->status = 2; $JSONResults->msg = "Возвращена стоимость доставки"; if (isset($oj->result)) { $JSONResults->price = $oj->result->price; } if (isset($oj->result->deliveryPeriodMin)) { $JSONResults->deliveryPeriodMin = $oj->result->deliveryPeriodMin; } if (isset($oj->result->deliveryPeriodMax)) { $JSONResults->deliveryPeriodMax = $oj->result->deliveryPeriodMax; } } } } echo json_encode($JSONResults); Библиотека cdekLib.php "15", "length"=>"40", "width"=>"50", "height"=>"60" ), ); } class cdekLib { public function curlQuery($postData=false) { $return = false;$uri = 1; if (isset($uri) and $uri and isset($postData) and $postData) { $ch = curl_init(); $host = "api.cdek.ru"; curl_setopt($ch, CURLOPT_URL, "https://$host" . "/calculator/calculate_price_by_json.php"); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $header[] = "Host: " . $host; $header[] = "User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:61.0) Gecko/20100101 Firefox/61.0"; $header[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; $header[] = "Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3"; $header[] = "Accept-Encoding: gzip, deflate, br"; $header[] = "Content-Type: application/json;charset=utf-8"; $header[] = "Connection: keep-alive"; $header[] = "Upgrade-Insecure-Request: 1"; curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch,CURLOPT_ENCODING , "gzip, deflate"); curl_setopt($ch,CURLOPT_RETURNTRANSFER , 1); // Не возвращать результат в браузер curl_setopt($ch, CURLOPT_HTTPHEADER, $header); $text = curl_exec($ch);// выполнить запрос, присвоить результат переменной curl_close($ch); if ($text = json_decode($text)) { $return = $text; } } return $return; } public function calc($id=false,$weight=false) { $return = false; if ($id and ((($id * 1) . "") == ($id . "")) and $weight and ((($weight * 1) . "") == ($weight . ""))) { $id = $id * 1;$weight = $weight * 1;// Получаем значения int $forRequest = new forRequest; $forRequest->dateExecute = date("Y-m-d",time()); $forRequest->receiverCityId = $id; $forRequest->weight = $weight; if ($obj = $this->curlQuery(json_encode($forRequest))) { $return = $obj; } } return $return; } public function search($text=false,$return=false) { if ($text) { global $DB; $text = $DB->ForSql($_REQUEST["text"]); $connection = Main\Application::getInstance()->getConnection(); $result = $connection->query("SELECT * FROM `cdek_city` WHERE `cityName` LIKE '$text%';"); $iii = -1; $array = array(); foreach ($result->fetchAll() as $v) { $iii++; $id = $v["cityCode"]; $name = $v["cityName"]; if ($v["region"] and $v["subRegion"]) { $name .= " ["; if ($v["region"]) { $name .= $v["region"]; } if ($v["subRegion"]) { if ($v["region"]) { $name .= "/"; } $name .= $v["subRegion"]; } $name .= "]"; } if ($iii < 15) { $array[] = array("id"=>$id,"name"=>$name); } } if (count($array)) { $return = $array; } } return $return; } }