Files
GestioneSpiaggia.it/library/App/Model/CurlHttpClient.php
T
2021-09-17 12:41:45 +02:00

85 lines
2.4 KiB
PHP

<?php
class App_Model_CurlHttpClient
{
protected $_httpClient = null;
protected $_urlNameServices;
public function __construct($urlName){
$this->_urlNameServices = $urlName;
$this->_httpClient = new Zend_Http_Client($urlName);
$this->_httpClient->setConfig(array(
'adapter' => 'Zend_Http_Client_Adapter_Curl',
'maxredirects' => 0,
'timeout' => 60,
'ssltransport' => 'ssl'
));
}
/***** METODI COMUNI DELLE CHIMATE *****/
public function getCall($params){
$this->_httpClient->setMethod('GET');
$this->_httpClient->setParameterGet($params);
return $this->executeCall();
}
public function postCall($dati,$formatJson = false){
$this->_httpClient->setMethod('POST');
if($formatJson)
$this->_httpClient->setRawData(json_encode($dati),'application/json;charset=utf-8');
else
$this->_httpClient->setParameterPost($dati);
return $this->executeCall();
}
public function putCall($dati,$formatJson = false){
$this->_httpClient->setMethod('PUT');
if($formatJson)
$this->_httpClient->setRawData(json_encode($dati),'application/json;charset=utf-8');
else
$this->_httpClient->setParameterPost($dati);
return $this->executeCall();
}
public function deleteCall($params){
$this->_httpClient->setMethod('DELETE');
$this->_httpClient->setParameterGet($params);
return $this->executeCall();
}
/***** METODI PRIVATI PER GESTIRE LE CHIAMATE *****/
private function executeCall(){
$httpResponse = $this->_httpClient->request();
$headerResponse = $httpResponse->getHeaders();
if(preg_match('/application\/json/',$headerResponse['Content-type'])){
$httpResponseBody = json_decode($httpResponse->getBody(),1);
if(json_last_error() !== JSON_ERROR_NONE){
Zend_Registry::get('log')->info($this->_httpClient->getUri());
Zend_Registry::get('log')->info("Errore codifica JSON:");
Zend_Registry::get('log')->info(print_r($httpResponse,true));
return null;
}
if (!$httpResponseBody['status'] && !$httpResponseBody['success']) {
Zend_Registry::get('log')->info($this->_httpClient->getUri());
Zend_Registry::get('log')->info("Errore 'httpResponse' no status or success code:");
Zend_Registry::get('log')->info(print_r($httpResponseBody,true));
throw new Exception($httpResponseBody['message'],$httpResponseBody['errorCode']);
}
}else{
$httpResponseBody = $httpResponse->getBody();
}
return $httpResponseBody;
}
}