怎么利用PHP发送彩信

彩信相比与短信而言,除了他们都具有短信的共性能发送文字信息以外,彩信还可以配上丰富的文字,声音和动画,这样用户接收到短信的时候图文并茂,更加的生动直观,从而进一步的提高了用户对信息内容的兴趣,大大提升了短信的阅读率。下面给大家介绍下怎么利用PHP发送彩信。

<?php
header("Content-type: text/html; charset=utf-8");
/* 彩信发送demo */
$action = 'sendimagetext';
$url = 'http://www.lokapi.cn/smsUTF8.aspx';
$username = '******';
$password = strtoupper(md5('******'));
$token = '******';
$timestamp = getMillisecond();
$sign = strtoupper(md5('action=' . $action . '&username=' . $username . '&password=' . $password . '&token=' . $token . '&timestamp=' . $timestamp));
$title = '祝福短信';
$mobile = '17712345678';

/*构造发送主体 */
$content = '祝你生日快乐';
$txt = base64_encode(get_utf8_to_gb($content));
$path = "D:/我的文档/Pictures/6b76a7cb.jpg";
$extension = "jpg"; //图片后缀
$img = imgtobase64($path);
$message = 'txt|' . $txt . ',' . $extension . '|' . $img . ';'; //如果只发送一张图片最后这个;一定不要丢掉
$message = str_replace('%', '%25', $message);
$message = str_replace('&', '%26', $message);
$message = str_replace('+', '%2B', $message);

/*构造发送参数 */
$postData = array(
    'action' => $action,
    'username' => $username,
    'password' => $password,
    'token' => $token,
    'timestamp' => $timestamp,
    'sign' => $sign,
    'rece' => 'json',
    'title' => $title,
    'mobile' => $mobile,
    'message' => $message
);

$result = postSMS($url, $postData);

echo $result;
function imgtobase64($img = '', $imgHtmlCode = true)
{
    $imageInfo = getimagesize($img);
    $base64 = "" . chunk_split(base64_encode(file_get_contents($img)));
    return  chunk_split(base64_encode(file_get_contents($img)));;
}
function get_utf8_to_gb($value)
{
    $value_1 = $value;
    $value_2   =   @iconv("utf-8", "gb2312//IGNORE", $value_1); //使用@抵制错误,如果转换字符串中,某一个字符在目标字符集里没有对应字符,那么,这个字符之后的部分就被忽略掉了;即结果字符串内容不完整,此时要使用//IGNORE
    $value_3   =   @iconv("gb2312", "utf-8//IGNORE", $value_2);

    if (strlen($value_1)   ==   strlen($value_3)) {
        return   $value_2;
    } else {
        return   $value_1;
    }
}
function postSMS($url, $postData)
{
    $row = parse_url($url);
    $host = $row['host'];
    $port = isset($row['port']) ? $row['port'] : 80;
    $file = $row['path'];
    $post = "";
    while (list($k, $v) = each($postData)) {
        $post .= rawurlencode($k) . "=" . rawurlencode($v) . "&";
    }
    $post = substr($post, 0, -1);
    $len = strlen($post);
    $fp = @fsockopen($host, $port, $errno, $errstr, 10);
    if (!$fp) {
        return "$errstr ($errno)\n";
    } else {
        $receive = '';
        $out = "POST $file HTTP/1.1\r\n";
        $out .= "Host: $host\r\n";
        $out .= "Content-type: application/x-www-form-urlencoded\r\n";
        $out .= "Connection: Close\r\n";
        $out .= "Content-Length: $len\r\n\r\n";
        $out .= $post;
        fwrite($fp, $out);
        while (!feof($fp)) {
            $receive .= fgets($fp, 128);
        }
        fclose($fp);
        $receive = explode("\r\n\r\n", $receive);
        unset($receive[0]);
        return implode("", $receive);
    }
}
function getMillisecond()
{
    list($t1, $t2) = explode(' ', microtime());
    return (float) sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值