PHP对接营销短信API接口代码示例

header("Content-type:text/html; charset=UTF-8");

//请检查环境是否 开启 curl init。
function Post($curlPost,$url){
        $curlPost = json_encode($curlPost, JSON_UNESCAPED_UNICODE); //将数组转换为JSON格式
        $headers = array(
            'Content-Type: application/json; charset=utf-8', //设置请求头
            'Content-Length: ' . strlen($curlPost) //设置请求体长度
        );
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); //设置请求头
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        $return_str = curl_exec($curl);
        curl_close($curl);
        return $return_str;
}

//短信接口地址
$target = "https://api.ihuyi.com/sms-yx/v1/batchSend";

$api_key = 'xxxxxxxxxxxxxx'; //请替换为您的APIKEY
$post_data = array(
        'api_id' => 'sms-yx-xxxxxxxx', //APIID(用户中心【文本短信】-【会员营销短信】-【产品总览】查看)
        'signature' => 'xxxxxxxxx', //请求验证加密签名(非短信签名);签名生成方式:仅公共参数以ASCII码从小到大排序值,key=value,多值以“&”隔开,拼接之后md5 32位小写; 如:md5(api_id=xxxx&api_key=xxxx&request_id=xxxxxxxx&timestamp=xxxxxxx)APIKEY(用户中心【文本短信】-【会员营销短信】-【产品总览】查看)2、动态密码(生成动态密码方式请看该文档末尾的说明)
        'timestamp' => 1623643787, //东八时区;10位时间戳,时间允许相差±60S
        'request_id' => 'xxxxxxxxxxxx', //请求方请求ID,建议使用唯一ID,比如使用uuid;我方系统会2小时内去重验证处理,防止网络重复攻击;
        'product_id' => 1001, //产品ID
        'phone' => array(0=>'18800000000',1=>'18800000001'), //手机号数组(最多1万个号码)
        'sign_name' => 'xxxxxxxx', //短信签名(template_id未填写则必填)
        'content' => '尊敬的会员您好:${name},您的订单号是:${order_no},拒收请回复R', //template_id为空时必填;短信内容,如:您的短信群发功能已开通,请在3个工作日之内至平台进行企业认证! 短信内容和模板ID必须传入1个;当短信内容和模板ID都传入时,传入内容生效,模板ID属性失效;
        'template_id' => 1, //模板ID(内容为空则必填)
        'template_var' => array('${name}'=>'张三','${order_no}'=>'202009041156181103'), //选择模板时,且模板是变量模板时,可以传入变量值,需要传入json格式;
        'send_time' => '2020-08-26 16:08:14', //定时发送时间
);

$post_data['timestamp'] = time();
$post_data['request_id'] = $post_data['timestamp'].rand(0,9999);
$signature = 'api_id=' . $post_data['api_id'] . '&' . 'api_key=' . $api_key . '&' . 'request_id=' . $post_data['request_id'] . '&' . 'timestamp=' . $post_data['timestamp'];
$post_data['signature'] = md5($signature);

$res = Post($post_data, $target);
print_r($res);