<?php
session_start();
header("Content-type:text/html; charset=UTF-8");
function https_request($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
if (curl_errno($curl)) {return 'ERROR '.curl_error($curl);}
curl_close($curl);
return $data;
}
function xml_to_array($xml){
$reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
if(preg_match_all($reg, $xml, $matches)){
$count = count($matches[0]);
for($i = 0; $i < $count; $i++){
$subxml= $matches[2][$i];
$key = $matches[1][$i];
if(preg_match( $reg, $subxml )){
$arr[$key] = xml_to_array( $subxml );
}else{
$arr[$key] = $subxml;
}
}
}
return @$arr;
}
if(empty($mobile)){
//exit('手机号码不能为空');
}
if(empty($_SESSION['send_code']) or $send_code!=$_SESSION['send_code']){
//防用户恶意请求
//exit('请求超时,请刷新页面后重试');
}
function percentEncode($str)
{
// 使用urlencode编码后,将"+","*","%7E"做替换即满足ECS API规定的编码规范
$res = urlencode($str);
$res = preg_replace('/\+/', '%20', $res);
$res = preg_replace('/\*/', '%2A', $res);
$res = preg_replace('/%7E/', '~', $res);
return $res;
}
function computeSignature($parameters, $accessKeySecret)
{
// 将参数Key按字典顺序排序
ksort($parameters);
// 生成规范化请求字符串
$canonicalizedQueryString = '';
foreach($parameters as $key => $value)
{
$canonicalizedQueryString .= '&' . percentEncode($key)
. '=' . percentEncode($value);
}
// 生成用于计算签名的字符串 stringToSign
$stringToSign = 'GET&%2F&' . percentencode(substr($canonicalizedQueryString, 1));
//echo "<br>".$stringToSign."<br>";
// 计算签名,注意accessKeySecret后面要加上字符'&'
$signature = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret . '&', true));
return $signature;
}
// 注意使用GMT时间
date_default_timezone_set("GMT");
$target = "https://dysmsapi.aliyuncs.com/?";
$dateTimeFormat = 'Y-m-d\TH:i:s\Z'; // ISO8601规范
$accessKeyId = 'XXXXXXXX'; // 这里填写您的Access Key ID
$accessKeySecret = 'XXXXXXXXXXXXXXXXX'; // 这里填写您的Access Key Secret
$ParamString="{\"XXName\":\"test\",\"XXXXType\":\"test\"}";
$data = array(
'AccessKeyId' => $accessKeyId,
'Action' => 'SendSms',
'Format' => 'XML',
'OutId' => '123',
'PhoneNumbers' => '183XXXXXXXX',
'RegionId' => 'cn-hangzhou',
'SignName'=>'XXXX电子商务有限公司',
'SignatureMethod' => 'HMAC-SHA1',
'SignatureNonce'=> uniqid(),
'SignatureVersion' => '1.0',
'TemplateCode' => 'SMS_9705XXXX',
'TemplateParam' => $ParamString,
'Timestamp' => date($dateTimeFormat),
'Version' => '2017-05-25'
);
// 计算签名并把签名结果加入请求参数
$data['Signature'] = computeSignature($data, $accessKeySecret);
// 发送请求
$result = xml_to_array(https_request($target.http_build_query($data)));
echo $result['Error']['Code']."--->".$result['Error']['Message'];
echo "<br><br>".$target . http_build_query($data);
?>
用post方式访问只需要改三处:
1、将computeSignature函数内的拼接字符GET改成POST
// 生成用于计算签名的字符串 stringToSign
$stringToSign = 'POST&%2F&' . percentencode(substr($canonicalizedQueryString, 1));
2、将发送http请求的PHP函数https_request改为用POST方式传:
function https_request($url)
{
$curl = curl_init();
//设置抓取的url
curl_setopt($curl, CURLOPT_URL, 'https://dysmsapi.aliyuncs.com');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
//设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_HEADER, 1);
//设置获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//设置post方式提交
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $url);
$data = curl_exec($curl);
if (curl_errno($curl)) {return 'ERROR '.curl_error($curl);}
curl_close($curl);
return $data;
}
3、调用https_request的参数需要调整为POST要传输的值
$result = xml_to_array(https_request(http_build_query($data)));
DONE!