smsq/includes/sms_gw_api.php

121 lines
2.8 KiB
PHP
Raw Permalink Normal View History

2017-11-15 05:23:00 +01:00
<?php
Class sms_gw_api {
// WebServices parameters
private $ws_url = null;
private $ws_ssl_verify = true;
private $ws_connect_timeout = 5;
private $ws_timeout = 10;
function sms_gw_api($ws_url, $ws_ssl_verify = true, $ws_connect_timeout = 5, $ws_timeout = 10){
2017-11-15 05:23:00 +01:00
$this -> ws_url = $ws_url;
$this -> ws_ssl_verify = $ws_ssl_verify;
$this -> ws_connect_timeout = $ws_connect_timeout;
$this -> ws_timeout = $ws_timeout;
2017-11-15 05:23:00 +01:00
}
/*
* 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);
}
// Set timeouts
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this -> ws_connect_timeout);
curl_setopt($curl, CURLOPT_TIMEOUT, $this -> ws_timeout);
2017-11-15 05:23:00 +01:00
$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));
}
}