class API { public $apiKey = '** key here **'; public $entryPoint = 'https://api.novaposhta.ua/v2.0/json/'; public function getRegions() { return $this->requestToAPI('getAreas'); } public function getCities() { return $this->requestToAPI('getCities'); } private function requestToAPI($method, $model = 'Address') { $params = array( 'modelName' => $model, 'calledMethod' => $method, 'apiKey' => $this->apiKey ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->entryPoint); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); curl_close($ch); $decodedResponse = json_decode($response, true); return $decodedResponse; } } 
  • For what purpose do you ask? what is not working? Or in terms of beauty and neatness of the code? - Kostiantyn Okhotnyk
  • I ask from the point of code implementation and its validity - Valera Bondarenko

2 answers 2

  1. The public fields of the $apiKey , $entryPoint not very correct.
  2. CURLOPT_SSL_VERIFYPEER, false it is still better to check CURLOPT_SSL_VERIFYPEER, false
  3. There is no check for curl_ errors. What if the host is not found or the request is not made correctly?
  4. Answers from the server can not be stupidly distilled into an array, but into a class (but this depends on the task).
  • Regarding the second point: if I check the certificate, then I get curl_exec === false - Valera Bondarenko
  • You get it right. It is necessary to substitute the certificate itself into the option CURLOPT_CAINFO - ArchDemon
  • Do I need to get the certificate from New Mail? - Valera Bondarenko
  • @ValeraBondarenko, stackoverflow.com/questions/23032165/… - ArchDemon

Alternatively, you can do it like this: Select an API abstraction:

 class Api { protected function send($url, $params) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if(curl_exec($ch) === false) { throw new Exception('Нет ответа от сервера'); } $response = curl_exec($ch); curl_close($ch); $decodedResponse = json_decode($response, true); return $decodedResponse; } } 

Inherit a class, because then you will need to receive not only from new mail, but also from other APIs and not just by post:

 class ApiNewPost extends Api { private $apiKey = '** key here **'; private $entryPoint = 'https://api.novaposhta.ua/v2.0/json/'; public function requestToAPI($method, $model = 'Address') { $params = array( 'modelName' => $model, 'calledMethod' => $method, 'apiKey' => $this->apiKey ); return $this->send($this->entryPoint, json_encode($params)); } } 

define the response interface from the API:

 interface ResponseApi { public function getData(); } 

And for regions and cities create separate classes:

 class Regions implements ResponseApi { private $api; function __construct(Api $api) { $this->api = $api; } public function getData() { return $this->api->requestToAPI('getAreas'); } } class Cities implements ResponseApi { private $api; function __construct(Api $api) { $this->api = $api; } public function getData() { return $this->api->requestToAPI('getCities'); } } 

Here is such an option

 $cities = new Cities(new ApiNewPost()); $regions = new Regions(new ApiNewPost());