smsq/includes/sms_gw_api.php
2017-11-15 05:24:35 +01:00

111 lines
2.5 KiB
PHP

<?php
Class sms_gw_api {
// WebServices parameters
private $ws_url = null;
private $ws_ssl_verify = true;
function sms_gw_api($ws_url, $ws_ssl_verify=true){
$this -> ws_url = $ws_url;
$this -> ws_ssl_verify = $ws_ssl_verify;
}
/*
* REST Request
*/
private function _ws_call($method, $url, $data = false, $json = true) {
if (is_null($this -> ws_url)) return;
$curl = curl_init();
switch ($method) {
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
$method='GET';
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
if ($this -> ws_ssl_verify) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
}
else {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
}
$complete_url=$this -> ws_url.'/'.$url;
curl_setopt($curl, CURLOPT_URL, $complete_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
logging("DEBUG", "sms_gw_api : Call URL $complete_url");
$result = curl_exec($curl);
if(curl_errno($curl)) {
logging("ERROR", "sms_gw_api : Error during $method request to ".$complete_url." : ".curl_error($curl));
curl_close($curl);
return False;
}
curl_close($curl);
logging("DEBUG","sms_gw_api : Return => '$result'");
if (!$json)
return $result;
try {
$data = json_decode($result,true);
logging("DEBUG","sms_gw_api : Decoded data :".print_r($data,1));
return $data;
}
catch (Exception $e) {
logging("ERROR","sms_gw_api : Error parsing $method request JSON result on ".$complete_url." : ".$e);
return False;
}
}
/**
* send sms
* @param string number
* @param string text
* @return struct sms gw return struct
*/
function send_sms($number, $text){
return self :: _ws_call(
'GET',
"",
array(
'number' => $number,
'text' => $text,
)
);
}
/**
* check gateway is alive
* @return boolean sms gw status
*/
function check_gateway(){
$return=self :: _ws_call(
'GET',
"",
array(),
false
);
return ($return !== false && preg_match('/My SMS Gateway/', $return));
}
}