ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
>[success] ## 四、请求示例 (php) >[success]#### 4.1 代码示例 ```php @ $mssage = json_encode(array('code'=>123456)); @ $appkey:你的账号account @ $appSecret:你的账号密钥secret public function sendKmdMsg($mobile, $message, $templateCode,$SignName,$appkey,$appSecret,$version="2019-01-01",$domai_url=) { $params = array (); // *** 需用户填写部分 *** // fixme 必填: 您的账号信息 $accessKeyId = $appkey; $accessKeySecret = $appSecret; // fixme 必填: 短信接收号码 $params["PhoneNumbers"] = $mobile; // fixme 必填: 短信签名 $params["SignName"] = $SignName; // fixme 必填: 短信模板Code $params["TemplateCode"] = $templateCode; // fixme 必填: 设置模板参数 $params['TemplateParam'] =json_decode($message,true); // *** 需用户填写部分结束, 以下代码若无必要无需更改 *** if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) { $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE); } // 初始化SignatureHelper实例用于设置参数,签名以及发送请求 $helper = new SignatureHelper(); // 此处可能会抛出异常,注意catch $content = $helper->request( $accessKeyId, $accessKeySecret, 'api2.kemanduo.net/v2/api/sms/send', array_merge($params, array( "RegionId" => "cn-hangzhou", "Action" => "SendSms", "Version" => $version, )) ); return $content; } } //发送 class SignatureHelper { /** * 生成签名并发起请求 * * @param $accessKeyId string AccessKeyId * @param $accessKeySecret string AccessKeySecret * @param $domain string API接口所在域名 * @param $params array API具体参数 * @param $security boolean 使用http 目前不支持https * @return bool|\stdClass 返回API接口调用结果,当发生错误时返回false */ 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)); //生成加密签名,签名采用HmacSHA1算法 + Base64 $sign = base64_encode(hash_hmac("sha1", $stringToSign, $accessKeySecret . "&",true)); $signature = $this->encode($sign); //签名加到URL,做成特殊的URL编码 $url = ($security ? 'https' : 'http')."://http://api2.kemanduo.net/v2/api/sms/send/?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; } ``` >[success]#### 4.2 返回状态码 |状态码|值|说明| |-|-|-| |Code|OK/(1)|成功| |Code|FAIL/(-1)|失败| |Code|其他|失败| >[success]#### 4.3 返回信息 |参数|值|说明| |-|-|-| |Message|...|返回具体信息| |RequestId|...|请求的信息ID| -------by joann