Sms.class.php 3.15 KB
<?php
require_once dirname(__FILE__).'/sms/config/config.php';
/**
 * 
 * @author Administrator
 *
 */
class Util_Sms
{
    /**
     * 
     * @var string
     */
    const MTARGET = SMS_SERVER_ADDR;
    
    /**
     * 发送文本信息
     * 
     * @param string $phoneNumber
     * @param string $content
     * @return boolean
     */
    public static function sendTextMessage($phoneNumber, $content)
    {
        if(empty($phoneNumber) || empty($content))
        {
            return false;
        }
        $postData = array(
                             //提交账户
                             'sname' => SMS_USER_NAME,
                             //提交账户密码
                             'spwd' => SMS_USER_PASSWORD,
                              //企业代码
                             'scorpid'=>'',
                             //产品编号
                             'sprdid'=> SMS_PRODUCT_ID2,
                             //接收号码,每次只能提交1个手机号码
                             'sdst'=> $phoneNumber,
                             //信息内容,通常为70汉字以内,超过70字,请先测试
                             'smsg'=> $content,
                           );               
        $result = self::post(http_build_query($postData), self::MTARGET);
        if(!empty($result))
        {
            $simpleXML = @simplexml_load_string($result);
            if(is_object($simpleXML))
            {
                
                /* 
                 * 0:成功
                 * -1:系统异常
                 * 6001:无效的帐号或密码
                 * 6012:当前的IP禁止查询
                 * 6021:无效的企业介入号
                 * 6024:帐号处于冻结状态
                 */
                $state = $simpleXML->State;
                if($state == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    
    /**
     * 发送数据
     * 
     * @param string $data
     * @param string $target
     * @return string
     */
    private static function post($data, $target) 
    {
        $url_info = parse_url($target);
        $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
        $httpheader .= "Host:" . $url_info['host'] . "\r\n";
        $httpheader .= "Content-Type:application/x-www-form-urlencoded\r\n";
        $httpheader .= "Content-Length:" . strlen($data) . "\r\n";
        $httpheader .= "Connection:close\r\n\r\n";
        //$httpheader .= "Connection:Keep-Alive\r\n\r\n";
        $httpheader .= $data;
        $fd = fsockopen($url_info['host'], 80);
        fwrite($fd, $httpheader);
        $gets = "";
        while(!@feof($fd)) 
        {
            $gets .= @fread($fd, 128);
        }
        fclose($fd);
        preg_match("/Content-Length:.?(\d+)/", $gets, $matches);
        $length = $matches[1];
        $gets = substr($gets, - $length);
        return $gets;
        
    }

}

?>