阿里云手机验证码获取接口【接口对接的时候要仔细把里面的参数换成自己的参数即可】
生活随笔
收集整理的這篇文章主要介紹了
阿里云手机验证码获取接口【接口对接的时候要仔细把里面的参数换成自己的参数即可】
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
以下是接口代碼
<?php/*** 生成簽名并發(fā)起請(qǐng)求* @param $accessKeyId string AccessKeyId (https://ak-console.aliyun.com/)* @param $accessKeySecret string AccessKeySecret* @param $domain string API接口所在域名* @param $params array API具體參數(shù)* @param $security boolean 使用https* @param $method boolean 使用GET或POST方法請(qǐng)求,VPC僅支持POST* @return bool|\stdClass 返回API接口調(diào)用結(jié)果,當(dāng)發(fā)生錯(cuò)誤時(shí)返回false*/function request($accessKeyId, $accessKeySecret, $domain, $params, $security=false, $method='POST') {$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 .= "&" . encode($key) . "=" . encode($value);}$stringToSign = "${method}&%2F&" . encode(substr($sortedQueryStringTmp, 1));$sign = base64_encode(hash_hmac("sha1", $stringToSign, $accessKeySecret . "&",true));$signature = encode($sign);$url = ($security ? 'https' : 'http')."://{$domain}/";try {$content = fetchContent($url, $method, "Signature={$signature}{$sortedQueryStringTmp}");return json_decode($content);} catch( \Exception $e) {return false;}}function encode($str){$res = urlencode($str);$res = preg_replace("/\+/", "%20", $res);$res = preg_replace("/\*/", "%2A", $res);$res = preg_replace("/%7E/", "~", $res);return $res;}function fetchContent($url, $method, $body) {$ch = curl_init();if($method == 'POST') {curl_setopt($ch, CURLOPT_POST, 1);//post提交方式curl_setopt($ch, CURLOPT_POSTFIELDS, $body);} else {$url .= '?'.$body;}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) {// 大多由設(shè)置等原因引起,一般無(wú)法保障后續(xù)邏輯正常執(zhí)行,// 所以這里觸發(fā)的是E_USER_ERROR,會(huì)終止腳本執(zhí)行,無(wú)法被try...catch捕獲,需要用戶排查環(huán)境、網(wǎng)絡(luò)等故障trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR);}curl_close($ch);return $rtn;}/*** 發(fā)送短信*/ function sendSms($mobile,$code) {$params = array ();// *** 需用戶填寫部分 ***// fixme 必填:是否啟用https$security = false;// fixme 必填: 請(qǐng)參閱 https://ak-console.aliyun.com/ 取得您的AK信息$accessKeyId = "";$accessKeySecret = "";// fixme 必填: 短信接收號(hào)碼 例如:15755129456$params["PhoneNumbers"] = $mobile;// fixme 必填: 短信簽名,應(yīng)嚴(yán)格按"簽名名稱"填寫,請(qǐng)參考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign$params["SignName"] = "";// fixme 必填: 短信模板Code$params["TemplateCode"] = "";// fixme 可選: 設(shè)置模板參數(shù), 假如模板中存在變量需要替換則為必填項(xiàng)$params['TemplateParam'] = Array ("code" => $code,"product" => "fruits");// fixme 可選: 設(shè)置發(fā)送短信流水號(hào)$params['OutId'] = "12345";// fixme 可選: 上行短信擴(kuò)展碼, 擴(kuò)展碼字段控制在7位或以下,無(wú)特殊需求用戶請(qǐng)忽略此字段$params['SmsUpExtendCode'] = "1234567";// *** 需用戶填寫部分結(jié)束, 以下代碼若無(wú)必要無(wú)需更改 ***if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {$params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);}// 初始化SignatureHelper實(shí)例用于設(shè)置參數(shù),簽名以及發(fā)送請(qǐng)求// 此處可能會(huì)拋出異常,注意catch$content = request($accessKeyId,$accessKeySecret,"dysmsapi.aliyuncs.com",array_merge($params, array("RegionId" => "cn-hangzhou","Action" => "SendSms","Version" => "2017-05-25",)),$security);return $content; }ini_set("display_errors", "on"); // 顯示錯(cuò)誤提示,僅用于測(cè)試時(shí)排查問(wèn)題 error_reporting(E_ALL); // 顯示所有錯(cuò)誤提示,僅用于測(cè)試時(shí)排查問(wèn)題 set_time_limit(0); // 防止腳本超時(shí),僅用于測(cè)試使用,生產(chǎn)環(huán)境請(qǐng)按實(shí)際情況設(shè)置 header("Content-Type: text/plain; charset=utf-8"); // 輸出為utf-8的文本格式,僅用于測(cè)試 // 以下兩個(gè)字段mobile為獲取短信的手機(jī)號(hào)碼,code為手機(jī)獲取的驗(yàn)證碼 $mobile = intval($_POST['mobile']); $code = intval(rand(100000,999999)); print_r(sendSms($mobile,$code));總結(jié)
以上是生活随笔為你收集整理的阿里云手机验证码获取接口【接口对接的时候要仔细把里面的参数换成自己的参数即可】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 关于插画师,大家不知道的“秘密”
- 下一篇: 网站服务器带宽2m怎么样,云服务器带宽2