PHP cannot send text messages when accessing Alibaba SMS
Mr 张
Mr 张 2018-07-26 09:18:28
0
3
1197

The source code is as follows. Please ask the master for guidance on how to fix the problem.

The page is very simple. The user submits the content and then sends it through Alibaba Cloud.

<?php
include_once("admin_head.php");
getadmin();
$action=isset($_GET['action'])?$_GET['action']:"list";
switch($action){
	
	
	case "qunfa":
		?>
        <div id='test'></div>
		<form style='width:420px;' action='?action=savequnfa' method='post'  onsubmit='return check()' class='myform' enctype='multipart/form-data' >
          <br>
         <span class='myspan' style='width:80px;'>号码:</span><textarea name='qunfahaoma' style='width:420px;height:300px;'>群发的号码请用“,”来间隔,否则将会失败!</textarea><br/><br/>
         <span class='myspan' style='width:80px;'>内容:</span><textarea name='about' style='width:420px;height:100px;'>请勿发敏感字!</textarea>
  		 <center><input type='submit' value='确认发送' class='submit'></center>
         
        </form>
        
		<?php
		
	$haoma=$_POST['qunfahaoma'];
	//$neirong=$_POST['about'];
	break;
	
	case "savequnfa":
	$haoma=$_POST['qunfahaoma'];
	//print($haoma)
				if($haoma)sendSms("{$_POST['qunfahaoma']}","{$_POST['about']}");
				//短信通知//
				//短信通知//
				//$sendSms=sendSms("{$_POST['qunfahaoma']}","{$_POST['about']}");
				//短信通知//
	
class SignatureHelper{
    public function request($accessKeyId, $accessKeySecret, $domain, $params, $security=false) {
        $apiParams = array_merge(array (
            "SignatureMethod" => "HMAC-SHA1",
            "SignatureNonce" => uniqid(mt_rand(0,0xffff), true),
            "SignatureVersion" => "1.0",
            "AccessKeyId" => $accessKeyId,
            "Timestamp" => gmdate("Y-m-d\TH:i:s\Z"),
            "Format" => "JSON",
        ), $params);
        ksort($apiParams);
        $sortedQueryStringTmp = "";
        foreach ($apiParams as $key => $value) {
            $sortedQueryStringTmp .= "&" . $this->encode($key) . "=" . $this->encode($value);
        }
        $stringToSign = "GET&%2F&" . $this->encode(substr($sortedQueryStringTmp, 1));
        $sign = base64_encode(hash_hmac("sha1", $stringToSign, $accessKeySecret . "&",true));
        $signature = $this->encode($sign);
        $url = ($security ? 'https' : 'http')."://{$domain}/?Signature={$signature}{$sortedQueryStringTmp}";
        try {
            $content = $this->fetchContent($url);
            return json_decode($content);
        } catch( \Exception $e) {
            return false;
        }
    }

    private function encode($str){
        $res = urlencode($str);
        $res = preg_replace("/\+/", "%20", $res);
        $res = preg_replace("/\*/", "%2A", $res);
        $res = preg_replace("/%7E/", "~", $res);
        return $res;
    }

    private function fetchContent($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "x-sdk-client" => "php/2.0.0"
        ));
        if(substr($url, 0,5) == 'https') {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        $rtn = curl_exec($ch);
        if($rtn === false) {
            trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR);
        }
        curl_close($ch);
        return $rtn;
    }
}
//短信群发2018年7月25日17:24:40
function sendSms($tomobile,$code) {
    $params = array ();
    $accessKeyId = "LTAIZbtSes4imVOP";
    $accessKeySecret = "4ncagqiD1b5Twm0wySraxx3Ei***oA";
    $params["PhoneNumbers"] = "18356666163";//原使用变量测试方便直接使用了号码
    $params["SignName"] = "九华一楼";//短信签名
    $params["TemplateCode"] = "SMS_140525190";//模板CODE
    $params['TemplateParam'] = Array (
        "code" => $code,
    );//模板参数  
    $params['OutId'] = "";// fixme 可选: 设置发送短信流水号  
    $params['SmsUpExtendCode'] = "";// fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段
    /******************自定义部分结束************************/	
    if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
        $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
    }
    $helper = new SignatureHelper();
    // 此处可能会抛出异常,注意catch
    $content = $helper->request(
        $accessKeyId,
        $accessKeySecret,
        "dysmsapi.aliyuncs.com",
        array_merge($params, array(
            "RegionId" => "cn-hangzhou",
            "Action" => "SendSms",
            "Version" => "2017-05-25",
        ))
        // fixme 选填: 启用https
        // ,true
    );
	
ini_set("display_errors", "on"); // 显示错误提示,仅用于测试时排查问题
// error_reporting(E_ALL); // 显示所有错误提示,仅用于测试时排查问题
set_time_limit(0); // 防止脚本超时,仅用于测试使用,环境请按实际情况设置
header("Content-Type: text/plain; charset=utf-8"); // 输出为utf-8的文本格式,仅用于测试

// 验证发送短信(SendSms)接口
print_r(sendSms());
    return $content;
}
		alert("操作成功!响应时间依据您的短信数量和短信通道信号,请耐心等待","?action=qunfa");
	break;
	
}
include_once("admin_foot.php");
?>
Mr 张
Mr 张

reply all(2)
A.PHP网站定制开发~Mr 张

Put yourself up

Summer

You can print the return result to see where the error occurred

  • reply I use echo or print to output but cannot output.
    Mr 张 author 2018-07-26 09:36:56
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template