云之讯
model
private function getResult($url, $body = null, $method)
{
$data = $this->connection($url,$body,$method);
if (isset($data) && !empty($data)) {
$result = $data;
} else {
$result = ‘没有返回数据‘;
}
return $result;
}
/**
* @param $url 请求链接
* @param $body post数据
* @param $method post或get
* @return mixed|string
*/
private function connection($url, $body,$method)
{
if (function_exists("curl_init")) {
$header = array(
‘Accept:application/json‘,
‘Content-Type:application/json;charset=utf-8‘,
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
if($method == ‘post‘){
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$body);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
curl_close($ch);
} else {
$opts = array();
$opts[‘http‘] = array();
$headers = array(
"method" => strtoupper($method),
);
$headers[]= ‘Accept:application/json‘;
$headers[‘header‘] = array();
$headers[‘header‘][]= ‘Content-Type:application/json;charset=utf-8‘;
if(!empty($body)) {
$headers[‘header‘][]= ‘Content-Length:‘.strlen($body);
$headers[‘content‘]= $body;
}
$opts[‘http‘] = $headers;
$result = file_get_contents($url, false, stream_context_create($opts));
}
return $result;
}
/**
单条发送短信的function,适用于注册/找回密码/认证/操作提醒等单个用户单条短信的发送场景
* @param $appid 应用ID
* @param $mobile 接收短信的手机号码
* @param $templateid 短信模板,可在后台短信产品→选择接入的应用→短信模板-模板ID,查看该模板ID
* @param null $param 变量参数,多个参数使用英文逗号隔开(如:param=“a,b,c”)
* @param $uid 用于贵司标识短信的参数,按需选填。
* @return mixed|string
* @throws Exception
*/
public function SendSms($appid,$templateid,$param=null,$mobile,$uid){
//填写在开发者控制台首页上的Account Sid
$accountSid =‘00c8f06b95b6dd774c8a49306fac9ac5‘;
//填写在开发者控制台首页上的Auth Token
$token =‘71634558685668f591af03f8226b2f21‘;
$url = ‘https://open.ucpaas.com/ol/sms/sendsms‘;
$body_json = array(
‘sid‘=>$accountSid,
‘token‘=>$token,
‘appid‘=>$appid,
‘templateid‘=>$templateid,
‘param‘=>$param,
‘mobile‘=>$mobile,
‘uid‘=>$uid,
);
$body = json_encode($body_json);
$data = $this->getResult($url, $body,‘post‘);
return $data;
}
controller
public function sms(){
$appid = "b934d9af50dc452b82a357ce60e4675d"; //应用的ID,可在开发者控制台内的短信产品下查看
$templateid = "385111"; //可在后台短信产品→选择接入的应用→短信模板-模板ID,查看该模板ID
$param = rand(1000,9999); //多个参数使用英文逗号隔开(如:param=“a,b,c”),如为参数则留空 验证码
$mobile = $_POST[‘yzmtel‘]; //手机号
$uid = "2d92c6132139467b989d087c84a365d8";
Cookie::set("param",$param,60);
Cookie::set("phone",$mobile,60);
$sms = $this->i->SendSms($appid,$templateid,$param,$mobile,$uid);
}
亿美
model
function http_request($url, $data)
{
$data = http_build_query($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
print_r($output);
return $output;
}
function signmd5($appId,$secretKey,$timestamp){
return md5($appId.$secretKey.$timestamp);
}
function SendSMS($rand)
{
$content = "【麦吉丽】您的验证码是".$rand;/* 短信内容请以商务约定的为准,如果已经在通道端绑定了签名,则无需在这里添加签名 */
$timestamp = date("YmdHis");
$sign = $this->signmd5(YM_SMS_APPID,YM_SMS_AESPWD,$timestamp);
// 如果您的系统环境不是UTF-8,需要转码到UTF-8。如下:从gb2312转到了UTF-8
// $content = mb_convert_encoding( $content,"UTF-8","gb2312");
// 另外,如果包含特殊字符,需要对内容进行urlencode
$data = array(
"appId" => YM_SMS_APPID,
"timestamp" => $timestamp,
"sign" => $sign,
"mobiles" => "15821150253",
"content" => $content,
"customSmsId" => "10001",
"timerTime" => "",
"extendedCode" => ""
);
$url = YM_SMS_ADDR.YM_SMS_SEND_URI;
$resobj = $this->http_request($url, $data);
return $resobj;
}
controller
public function SendSMS(){
$mobile = $_POST[‘u_phone‘];
$rand = rand(1000,9999);
Cookie::set("rand",$rand,60);
Cookie::set("mobile",$mobile,60);
$rs = $this->l->SendSMS($rand);
}
原文地址:https://www.cnblogs.com/xin521/p/9920525.html