<?php
class sms_notify
{
    private $soap = null;
    private $location = 'http://www.wemediacn.net/webservice/smsservice.asmx?WSDL';
    private $apitoken = '7100680230574222';
    private $phoneNums = array(13764286626, 17521168623);
    private $triggerFile = "/usr/lib/zabbix/alertscripts/sms/logfile/smsfile.log";
    private $smsLogFile = "sms_notify.log";

    public function sendSms(){
        if ($client = $this->getSmsSoap()) {
            $content = $this->getMsg();
            if(!$content){
                return;
            }

            foreach($this->phoneNums as $phoneNum){
                $params = array(
                    'mobile' => $phoneNum,
                    'FormatID' => 8,
                    'Content' => $content,
                    'ScheduleDate' => '2015-01-01',
                    'TokenID' => $this->apitoken
                );

                $result = $client->SendSMS($params);
                $this->saveToDebug($phoneNum, $content, $result->SendSMSResult);
            }
        }
    }

    public function getMsg(){
        $msg = file_get_contents($this->triggerFile);
        return $msg;
    }

    public function getSmsSoap()
    {
        if (is_null($this->soap)) {
            try {
                $soap = new SoapClient($this->location, array(
                    'exceptions' => true,
                    'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP,
                ));
                $this->soap = $soap;
            } catch (SoapFault $e) {
                $this->soap = null;
            }
        }

        return $this->soap;
    }

    public function saveToDebug($phoneNum, $content, $sendResult){
        $text = "-----".date('Y-m-d H:i:s')."-----\n接收号码为：" . $phoneNum . "\n发送内容为：\n" . $content . "\n返回码为：" . $sendResult."\n\n\n";
        file_put_contents($this->smsLogFile, $text, FILE_APPEND);
    }
}
date_default_timezone_set('Asia/Shanghai');
$sms = new sms_notify();
$sms->sendSms();

