MessageSendDemo


概览

加密签名计算方法请参考createSignature.php。


代码示列

<?php 

$version = "2";
$sign_type = "md5";
$appid = "appid";
$appkey = "appkey";
$product = "message";
$to_address = "152xxxxxxxxx";
$url = 'http://api.mysubmail.com/'.$product.'/send.json';


if($sign_type != "md5" && $sign_type != "sha1"){
    $post_data = array(
        "appid" => $appid,
        "signature"=>$appkey,
        "to" => $to_address,
        "content"=>"【SUBMAIL】尊敬的@var(name),我是SUBMAIL",
    );
}else{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://api.mysubmail.com/service/timestamp.json');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 0);

    $output = curl_exec($ch);
    curl_close($ch);
    $output = json_decode($output,true);
    $timestamp = $output['timestamp'];
    $post_data = array(
        "appid" => $appid,
        "to" => $to_address,
        "timestamp"=>$timestamp,
        "sign_type"=>$sign_type,
        "sign_version"=>$version,
        "content"=>"【SUBMAIL】尊敬的@var(name),我是SUBMAIL",
    );
    $temp = $post_data;
    if($version == '2'){
        unset($temp['content']);
    }else{
        unset($temp['sign_version']);
    }
    ksort($temp);
    reset($temp);
    $tempStr = "";
    foreach ($temp as $key => $value) {
        $tempStr.=$key."=".$value."&";
    }
    $tempStr = substr($tempStr,0,count($tempStr)-2);
    if($sign_type == "md5")
        $post_data['signature'] = md5($appid.$appkey.$tempStr.$appid.$appkey);
    else
        $post_data['signature'] = sha1($appid.$appkey.$tempStr.$appid.$appkey);

}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

$output = curl_exec($ch);
curl_close($ch);
echo json_encode($output);