云之讯发送短信验证码、短信通知完整函数示例。
云之讯所需配置项:
- clientid(子账号id)
- 每个短信通知的模板id
下面只要填写一下短信模板id和clientid即可使用。之所以没有封装到类文件,主要最近迷上写函数了。于是简单的封装一下,使用起来也特别简单。
<?php
const UCPAAS_BASE_URL = 'http://open2.ucpaas.com/sms-server/';
/**
*
* @remark
* @param $url
* @param $body
* @param $method
* @return bool|string
*/
function ucpass_request_post($url, $body, $method = 'post')
{
$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);
return $result;
}
/**
*
* clientid 子账号id
* @return string
*/
function ucpass_get_clientid()
{
return "";
}
/**
*
* 密码
* @return string
*/
function ucpass_get_passwrod()
{
return md5('12345678');
}
/**
*
* 请求uri
* @return string
*/
function ucpass_get_url()
{
return UCPAAS_BASE_URL . 'templatesms';
}
/**
*
* 验证码模板id
* @return string
*/
function ucpass_get_code_template_id()
{
return "";
}
/**
*
* 新订单模板id
* @return string
*/
function ucpass_get_new_order_template_id()
{
return "";
}
/**
*
* 审核通过通知模板id
* @return string
*/
function ucpass_get_check_pass_template_id()
{
return "";
}
/**
*
* 审核未通过模板id
* @return string
*/
function ucpass_get_check_refuse_template_id()
{
return "";
}
/**
*
* 验证码短信
* @return bool|array
*/
function ucpass_send_captcha($mobile, $code)
{
$body_json = [
'clientid' => ucpass_get_clientid(),
'password' => ucpass_get_passwrod(),
'templateid' => ucpass_get_code_template_id(),
'param' => $code,
'mobile' => $mobile,
'uid' => '',
];
$body = json_encode($body_json);
return json_decode(ucpass_request_post(ucpass_get_url(), $body), true);
}
/**
*
* 发送新订单通知
* @param $mobile
* @return mixed
*/
function ucpass_send_new_order($mobile)
{
$body_json = [
'clientid' => ucpass_get_clientid(),
'password' => ucpass_get_passwrod(),
'templateid' => ucpass_get_code_template_id(),
'param' => '',
'mobile' => $mobile,
'uid' => '',
];
$body = json_encode($body_json);
return json_decode(ucpass_request_post(ucpass_get_url(), $body), true);
}
/**
*
* 发送审核通过通知
* @param $mobile
* @return mixed
*/
function ucpass_send_check_pass($mobile)
{
$body_json = [
'clientid' => ucpass_get_clientid(),
'password' => ucpass_get_passwrod(),
'templateid' => ucpass_get_code_template_id(),
'param' => '',
'mobile' => $mobile,
'uid' => '',
];
$body = json_encode($body_json);
return json_decode(ucpass_request_post(ucpass_get_url(), $body), true);
}
/**
*
* 发送失败检查通知
* @param $mobile
* @return mixed
*/
function ucpass_send_check_refuse($mobile)
{
$body_json = [
'clientid' => ucpass_get_clientid(),
'password' => ucpass_get_passwrod(),
'templateid' => ucpass_get_code_template_id(),
'param' => '',
'mobile' => $mobile,
'uid' => '',
];
$body = json_encode($body_json);
return json_decode(ucpass_request_post(ucpass_get_url(), $body), true);
}