diff --git a/src/psm/Txtmsg/GatewayAPI.php b/src/psm/Txtmsg/GatewayAPI.php new file mode 100644 index 00000000..46138197 --- /dev/null +++ b/src/psm/Txtmsg/GatewayAPI.php @@ -0,0 +1,96 @@ +. + * + * @package phpservermon + * @author Ward Pieters + * @copyright Copyright (c) 2008-2017 Pepijn Over + * @license http://www.gnu.org/licenses/gpl.txt GNU GPL v3 + * @version Release: @package_version@ + * @link http://www.phpservermonitor.org/ + **/ + +namespace psm\Txtmsg; + +class GatewayAPI extends Core { + + /** + * Send sms using GatewayAPI + * @var string $message + * @var array $this->recipients + * @var string $recipient + * @var string $this->password + * @var string $this->originator + * @var int $success + * @var string $error + * @return int or string + */ + + public function sendSMS($message) { + $sender = $this->originator; + $apikey = $this->password; + $userref = $this->userref; + $destaddr = $this->destaddr; + $recipients = $this->recipients; + + $errors = array(); + + if(is_null($destaddr)) $destaddr = 'MOBILE'; + if(is_null($userref)) $userref = ''; + + + foreach($recipients as $recipient) { + $json = [ + 'sender' => $sender, + 'message' => $message, + 'recipients' => [], + 'destaddr' => $destaddr, + 'userref' => $userref, + ]; + + $json['recipients'][] = ['msisdn' => $recipient]; + + $ch = curl_init(); + curl_setopt($ch,CURLOPT_URL, "https://gatewayapi.com/rest/mtsms"); + curl_setopt($ch,CURLOPT_HTTPHEADER, array("Content-Type: application/json")); + curl_setopt($ch,CURLOPT_USERPWD, $apikey.":"); + curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($json)); + curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); + + $result = curl_exec($ch); + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + $result = json_decode($result,true); + $addidtional_information = array(); + foreach($result as $key => $value) { if($key != 'ids') $addidtional_information[$key] = $value; } + + if($httpcode == 200) { + // SMS is gelukt + $output = array('success' => true, 'ids' => $result['ids'], 'addidtional_information' => $addidtional_information); + } + else { + // SMS is niet gelukt + $output = array('success' => false, 'code' => $result['code'], 'message' => $result['message'], 'variables' => $result['variables']); + array_push($errors, $output); + } + } + if(!empty($errors)) return false; + else return true; + } +}