文档概览

短信验证码

php
			

<?php

/**
 * @param 通过CURL方法发送HTTP请求
 * @param string $url //请求URL
 * @param array|string $post_data   //请求参数
 */

function curl_sendrequest($url, $post_data)

 
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);    //获取url重定向
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //返回数据是否自动显示

 curl_setopt($ch, CURLOPT_POST, 1);
 if (is_array($post_data)) {
  $post_data = http_build_query($post_data);  //传入数组
 }
 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
 curl_setopt($ch, CURLOPT_HEADER, true);

 $response = curl_exec($ch);
 $curl_errno = (int)curl_errno($ch);
 if ($curl_errno !== 0) {
  $response = $curl_errno;
 } else {
  $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);  //HTTP请求头发送字节大小
  $header_map = [];
  foreach (explode("\r\n", substr($response, 0, $header_size)) as $header_line)
  {
   $header_info = explode(': ', $header_line, 2);
   if (count($header_info) === 2) {
    $header_map[$header_info[0]][] = $header_info[1];
   }
  }
  $response = array_merge([
   'http_code' => (int)curl_getinfo($ch, CURLINFO_HTTP_CODE),
   'header' => $header_map,
  ], strlen($response) > $header_size ? [ 'body' => substr($response, $header_size) ] : []);
 }
 curl_close($ch);
 return $response;
}


// 帐号
$user = '<此处填写用户名>';
$passwd = '<此处填写密码>';     
$url = 'http://XXX.XXX.XXX.XXX:7891/mt?';  //接入平台地址

// 发送短信
$message = '【广州昊博】您好!这里是广州昊博,竭诚为您服务!测试发送'; // 填写测试短信
$mobile = 13400000000; // 填写测试手机号

$post_data = [
 'un' => $user,  
 'pw' => $passwd,  
 'da' => $mobile,  
 'sm' => bin2hex(iconv("UTf-8", "GB2312", $message)), //选择字符编码
 'dc' => 15,   
 'rd' => 0   
];

$resp = curl_sendrequest($url, $post_data);
$success = explode('=', $resp['body'])[0] === 'id';
if ($success) {
 echo "SUCCEEDED!!\n";
}

var_dump($resp);

 

收缩