php 秒嘀短信验证码

ACCOUNT_SID; $AUTH_TOKEN=$this->AUTH_TOKEN; $BASE_URL=$this->BASE_URL; $CONTENT_TYPE=$this->CONTENT_TYPE; $ACCEPT=$this->ACCEPT; // 生成body $body = $this->createBasicAuthData($ACCOUNT_SID,$AUTH_TOKEN); // 在基本认证参数的基础上添加短信内容和发送目标号码的参数 $body['smsContent'] = "【帮你下】您的验证码为1111,请于5分钟内正确输入,如非本人操作,请忽略此短信。"; //这里内容要与秒嘀设置的文本内容相同 //还有就是我犯了一个致命错误,忘记改编码,会造成与内容模板不符 $body['to'] = ''; //手机号 // 提交请求 $result = $this->post($funAndOperate, $body,$BASE_URL,$ACCOUNT_SID,$AUTH_TOKEN,$CONTENT_TYPE,$ACCEPT); echo("
result:

"); var_dump($result); } function createUrl($funAndOperate,$BASE_URL,$ACCOUNT_SID,$AUTH_TOKEN) { $BASE_URL=$BASE_URL;$ACCOUNT_SID=$ACCOUNT_SID;$AUTH_TOKEN=$AUTH_TOKEN; // 时间戳 date_default_timezone_set("Asia/Shanghai"); $timestamp = date("YmdHis"); return $BASE_URL . $funAndOperate; } function createSig() { global $ACCOUNT_SID; $AUTH_TOKEN; $timestamp = date("YmdHis"); // 签名 $sig = md5($ACCOUNT_SID . $AUTH_TOKEN . $timestamp); return $sig; } function createBasicAuthData($ACCOUNT_SID,$AUTH_TOKEN) { $ACCOUNT_SID=$ACCOUNT_SID; $AUTH_TOKEN=$AUTH_TOKEN; $timestamp = date("YmdHis"); // 签名 $sig = md5($ACCOUNT_SID . $AUTH_TOKEN . $timestamp); return array("accountSid" => $ACCOUNT_SID, "timestamp" => $timestamp, "sig" => $sig, "respDataType"=> "JSON"); } /** * 创建请求头 * @param body * @return */ function createHeaders($CONTENT_TYPE,$ACCEPT) { $CONTENT_TYPE=$CONTENT_TYPE; $ACCEPT=$ACCEPT; $headers = array('Content-type: ' . $CONTENT_TYPE, 'Accept: ' . $ACCEPT); return $headers; } /** * post请求 * * @param funAndOperate * 功能和操作 * @param body * 要post的数据 * @return * @throws IOException */ function post($funAndOperate, $body,$BASE_URL,$ACCOUNT_SID,$AUTH_TOKEN,$CONTENT_TYPE,$ACCEPT) { // 构造请求数据 $url = $this->createUrl($funAndOperate,$BASE_URL,$ACCOUNT_SID,$AUTH_TOKEN); $headers = $this->createHeaders($CONTENT_TYPE,$ACCEPT); echo("url:
" . $url . "\n"); echo("

body:
" . json_encode($body)); echo("

headers:
"); var_dump($headers); //print_r($body); // 要求post请求的消息体为&拼接的字符串,所以做下面转换 $fields_string = ""; foreach ($body as $key => $value) { $fields_string .= $key . '=' . $value . '&'; } rtrim($fields_string, '&'); // 提交请求 $con = curl_init(); curl_setopt($con, CURLOPT_URL, $url); curl_setopt($con, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($con, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($con, CURLOPT_HEADER, 0); curl_setopt($con, CURLOPT_POST, 1); curl_setopt($con, CURLOPT_RETURNTRANSFER, 1); curl_setopt($con, CURLOPT_HTTPHEADER, $headers); curl_setopt($con, CURLOPT_POSTFIELDS, $fields_string); $result = curl_exec($con); curl_close($con); return "" . $result; } } $ll=new Aa(); $ll->sms();

你可能感兴趣的:(thinkphp,php秒嘀短信验证码)