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

79 lines
2.7 KiB
PHP

<?php
abstract class App_Model_FcmFirebase
{
const BASE_API_URL = "https://fcm.googleapis.com";
const API_VERSION = "fcm";
const SECRET_ID = "AAAAfg7FWPQ:APA91bHte0qwGR5J8wdfuUWJhU6-ehj_xXH6cOV6s2IFabm-86wBIwwUJsvwPmKD65PQ2HEl8SRzcy6EOl3GCc9K_StPgNpG8t84Zz5j7_uFKPCiHvblAb0xLk2eYYDsim4eBhvokv0s";
protected $_httpClient = null;
public function __construct()
{
if ($this->_httpClient instanceof Zend_Http_Client) {
//$this->_httpClient = $this->_httpClient;
} else {
$this->_httpClient = new Zend_Http_Client();
$this->_httpClient->setConfig(array(
'adapter' => 'Zend_Http_Client_Adapter_Curl',
'maxredirects' => 0,
'timeout' => 60,
'ssltransport' => 'ssl'
));
$this->_httpClient->setHeaders('Authorization', 'key=' . self::SECRET_ID);
}
}
/***** METODI COMUNI DELLE CHIMATE *****/
public function postCall($dati)
{
$this->_httpClient->setMethod('POST');
$this->_httpClient->setUri($this->getBaseUrl() . '/send');
$this->_httpClient->setRawData(Zend_Json::encode($dati), 'application/json;charset=utf-8');
return $this->executeCall();
}
/***** METODI PRIVATI PER GESTIRE LE CHIAMATE *****/
private function getBaseUrl()
{
return self::BASE_API_URL . "/" . self::API_VERSION;
}
private function executeCall()
{
$httpResponse = $this->_httpClient->request();
/*if($this->_ipManager->allowedIp()){
Zend_Registry::get('log')->info(print_r($httpResponse,true));
}*/
// Zend_Registry::get('log')->info(print_r($this->_httpClient->getLastRequest(), true));
$httpResponseEncoded = json_decode($httpResponse->getBody(), 1);
// Zend_Registry::get('log')->info(print_r($httpResponseEncoded, true));
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 ($this->_httpClient->getLastResponse()->getStatus() != '200' || $httpResponseEncoded['status'] == 'error') {
Zend_Registry::get('log')->info($this->_httpClient->getUri());
Zend_Registry::get('log')->info("Errore status response " . $this->_httpClient->getLastResponse()->getStatus());
Zend_Registry::get('log')->info(print_r($httpResponseEncoded, true));
throw new App_Exception_ResponseCloudException($httpResponseEncoded['message'], $httpResponseEncoded['errorCode']);
}
return $httpResponseEncoded;
}
}