php 发送手机短信(转载)

<?php  
    /*- 
     * 版权没有,欢迎拷贝 (x) 2006 胖头鱼. 
     * 作者: pangty <[email protected]> 
     * 
     * $Id: RS232.php,v 0.1 2006-7-29 05:28 PM Exp $ 
     * 
     * 运行环境:Windows XP,PHP 5.1.4 (cli),MySQL 
     * php.ini 
     * extension=php_mysql.dll 
     * extension=php_dio.dll 
     * 硬件要求:熊猫 PM818A GSM/GPRS MODEM或其他任何GSM MODEM模块 
     * 相关知识:GSM AT指令集和PDU格式,去google吧 
     *  
     * mysql> use smsc; 
     * mysql> desc sm_submit; 
     * +----------+----------------------+------+-----+---------+----------------+ 
     * | Field    | Type                 | Null | Key | Default | Extra          | 
     * +----------+----------------------+------+-----+---------+----------------+ 
     * | smid     | int(11) unsigned     |      | PRI | NULL    | auto_increment | 
     * | OrgAddr  | varchar(11)          | YES  |     | NULL    |                | 
     * | DestAddr | varchar(11)          |      | PRI |         |                | 
     * | Time     | datetime             | YES  |     | NULL    |                | 
     * | USERDATA | mediumtext           | YES  |     | NULL    |                | 
     * | stat     | smallint(3) unsigned | YES  | MUL | NULL    |                | 
     * +----------+----------------------+------+-----+---------+----------------+ 
     * 
     */  
    function gb2unicode($str) {  
            return iconv("gb2312", "UCS-2", $str);  
    }  
    function hex2str($hexstring) {  
            $str = '';  
            for($i=0; $i<strlen($hexstring); $i++){  
                    $str .= sprintf("%02X",ord(substr($hexstring,$i,1)));  
            }  
            return $str;  
    }  
    function InvertNumbers($msisdn) {  
            $len = strlen($msisdn);  
            if ( 0 != fmod($len, 2) ) {  
                    $msisdn .= "F";  
                    $len = $len + 1;  
            }  
            for ($i=0; $i<$len; $i+=2) {  
                    $t = $msisdn[$i];  
                    $msisdn[$i] = $msisdn[$i+1];  
                    $msisdn[$i+1] = $t;  
            }  
            return $msisdn;  
    }  
    /*---------------主程序开始---------------*/  
    set_time_limit(0);  
    $DEBUG = 0;  
    exec('mode COM1: baud=115200 data=8 stop=1 parity=n xon=on');  
    $fd = dio_open('COM1:', O_RDWR);  
    if(!$fd)  
    {  
            die("打开串口COM1失败");  
    }  
    $ff = dio_stat($fd);  
    print_r($ff);  
    echo "GSMAT is start on COM1\n";  
    // 短信中心号码  
    $smsc = "8613800538500";  
    // 短信最大长度70个汉字,Unicode表示需要280个字节  
    $max_len = 280;  
    $invert_smsc = InvertNumbers($smsc);  
    $sql = mysql_connect("localhost", "pangty", "pangty");  
    mysql_select_db("smsc",$sql);  
    $query = "select * from sm_submit where stat=0";  
    $result = mysql_query($query, $sql);  
    $len = 1; $s = chr(13);  
    while ( $row = mysql_fetch_object($result) ) {  
            $msisdn = "86". $row->DestAddr;  
            $sms_text = $row->USERDATA;  
              
            if ($DEBUG) echo date("Y-m-d H:i:s")." DEBUG $row->DestAddr\n";  
            if ($DEBUG) echo date("Y-m-d H:i:s")." DEBUG $sms_text\n";  
              
            $pdu_text = hex2str(gb2unicode($sms_text));  
            $invert_msisdn = InvertNumbers($msisdn);  
              
            // 拆分发送超过70汉字的短信(todo: 没有判断全英文的情况)  
            do {  
                    $pdu_len = strlen($pdu_text);  
                    if ( $pdu_len > $max_len ) {  
                            $pdu_text1 = substr($pdu_text, 0, $max_len);  
                            $pdu_text = substr($pdu_text, $max_len, $pdu_len - $max_len);  
                    } else {  
                            $pdu_text1 = $pdu_text;  
                            $pdu_text = "";  
                    }  
              
                    $pdu_len1 = sprintf("%02X", strlen($pdu_text1)/2);  
                    $pdu_text1 = $pdu_len1 . $pdu_text1;  
              
                    $pdu_text1 = "11000D91" . $invert_msisdn ."000800" . $pdu_text1;  
              
                    $atcmd = "AT+CMGS=" . sprintf("%d", strlen($pdu_text1)/2) . chr(13);  
                    $l = strlen($atcmd);  
                    $ll = @dio_write($fd,$atcmd);  
                    while ($l != $ll) {  
                            sleep(10);  
                            $ll = @dio_write($fd,$atcmd);  
                    }  
                    if ($DEBUG) echo date("Y-m-d H:i:s")." DEBUG $atcmd\n";  
                    do {  
                            $data = dio_read($fd, $len);  
                            echo $data;  
                    } while ( $data != $s );  
                    sleep(1);  
              
                    $pdu_text1 = "0891" . $invert_smsc . $pdu_text1 . chr(26).chr(13);  
                    $l = strlen($pdu_text1);  
                    $ll = @dio_write($fd,$pdu_text1);  
                    while ($l != $ll) {  
                            sleep(10);  
                            $ll = @dio_write($fd,$pdu_text1);  
                    }  
                    if ($DEBUG) echo date("Y-m-d H:i:s")." DEBUG $pdu_text1\n";  
                    do {  
                            $data = dio_read($fd, $len);  
                            echo $data;  
                    } while ( $data != $s );  
                      
                    sleep(7);  
            } while ( $pdu_text != "" );  
              
            mysql_query("update sm_submit set stat=1 where smid=$row->smid", $sql);  
    }  
    mysql_close($sql);  
    dio_close($fd);  
?>

 转自:http://blog.csdn.net/nailwl/archive/2010/03/23/5406824.aspx

php 发送手机短信(转载)

php

相关推荐