CURL PHP – это библиотека предназначенная для получения и передачи данных через такие протоколы, как HTTP, FTP, HTTPS. Библиотека используется для получения данных в виде XML, JSON и непосредственно в HTML, парсинга, загрузки и передачи файлов и т.д.
class Fivepost { // private $token_url = ""; private $tariff_url = ""; private $city_url = ""; private $points_url = ""; // private $id; private $secret; private $handle; public $token; public function __construct() { // $this->id = $id; $this->secret = ''; } public function init() { $this->handle = curl_init(); curl_setopt($this->handle, CURLOPT_RETURNTRANSFER, true); curl_setopt($this->handle, CURLOPT_POST, 1); curl_setopt($this->handle, CURLOPT_CONNECTTIMEOUT, 30); } public function token() { $fields = array( ); return $this->executeRequest($this->token_url.$this->secret, $fields, 'http'); } public function calculate($fields) { return $this->executeRequest($this->tariff_url, $fields); } public function getpoints($fields) { return $this->executeRequest($this->points_url, $fields, "post"); } public function getCity($fields) { return $this->executeRequest($this->city_url, $fields, "get"); } public function close() { curl_close($this->handle); } private function executeRequest($url, $fields, $type = 'json') { $header = array(); $header[] = "Accept: application/json"; if($this->token && $type != 'http') { $header[] = "Content-Type: application/json; charset=utf-8"; $header[] = "Authorization: Bearer " . $this->token; } curl_setopt($this->handle, CURLOPT_HTTPHEADER, $header); if($type == 'get') { curl_setopt($this->handle, CURLOPT_URL, $url .'?'. $fields); curl_setopt($this->handle, CURLOPT_POST, 0); } else { curl_setopt($this->handle, CURLOPT_URL, $url); } if ($fields != null) { if($type == 'json') { curl_setopt($this->handle, CURLOPT_POSTFIELDS, json_encode($fields)); } elseif($type == 'http') { curl_setopt($this->handle, CURLOPT_POSTFIELDS, http_build_query($fields)); } } $result = $this->exec(); return $result; } private function exec() { $result = curl_exec($this->handle); $result = json_decode($result, true); $info = curl_getinfo($this->handle); if ($info['http_code'] == 429) { $result['error'] = 429; } elseif ($info['http_code'] != 200) { $result['error'] = $info['http_code']; } return $result; } }
Из админки opencart как обратиться к api (catalog/controller/api) на тестовом сайта с http-авторизацией
$curl = curl_init(HTTPS_CATALOG ."index.php?route=api/retailcrm_callback/getOrderFromRetail&token=".$this->session->data['token']."&order_id=12961"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Authorization: Basic ' . base64_encode('login:password') )); $result = curl_exec($curl); curl_close($curl);