免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1092 | 回复: 0
打印 上一主题 下一主题

一个短信的类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-01-18 09:16 |只看该作者 |倒序浏览

    /* public connection parameters */
    var $SrvHost = "212.95.130.225";  //短信服务器地址
    var $Port = "8010"; //短信服务器端口
     
    /* private parameters */
    var $Id = ""; //连接Id
    var $Password = ""; //连接密码
    var $fp = "";
    var $Fee = 0; //短信收费类型:0-免费;1-按条收费;2-包月封顶收费
    var $errno = 0;
    var $errstr = "";
    var $Loggedin = false;
    /* constructor */
    function SMS($id,$passwd)
    {
$this->Id = $id;
$this->Password = $passwd;
$this->fp = fsockopen($this->SrvHost,$this->Port,$this->errno,$this->errstr);
if(!$this->fp)
     $this->halt("无法建立连接");
    }
     
    /* private Login as sender:should be called before SendMsg()*/
    function SLogin()
    {
  
fputs($this->fp,"SLogin ".$this->Id.",".$this->Password."\r\n");
$tempstr = fgets($this->fp,20);
if(!ereg("OK",$tempstr))
     $this->halt("登陆错误!");   
$this->Loggedin = true;
    }
     
    /* private Login as recipient:should be called before Receive*/
    function RLogin()
    {
  
fputs($this->fp,"RLogin ".$this->Id.",".$this->Password."\r\n");
$tempstr = fgets($this->fp,20);
echo $tempstr;
if(!ereg("OK",$tempstr))
     $this->halt("登陆错误!");     
    }
     
    /* public SendMessage */
    function SendMsg($Mobile,$Msg,$format,$OrderNo = 0)
    {
global $smsfreeid;
if(!$this->Loggedin)
     $this->SLogin(); //Login
$Msg = rawurlencode(trim($Msg));
$command = "Send Id=".$this->Id."&pwd=".$this->Password."&Mobile=$Mobile&Fmt=$format&Msg=$Msg";
if($this->Id != $smsfreeid)
     $command .= "&Fee=1&FeeNo=$Mobile";
else
     $command .= "&Fee=0";
if($OrderNo)
     $command .= "&OrderNo=$OrderNo";
$command .= "\r\n";
  
//echo $command;
fputs($this->fp,$command);
$tempstr = fgets($this->fp,20);
  
if(ereg("OK",$tempstr))
     return true;
      
elseif(ereg("Error 1",$tempstr))
     $this->halt("Id号无法识别");
      
elseif(ereg("Error 1",$tempstr))
     $this->halt("非法IP地址");
      
elseif(ereg("No Msg",$tempstr))
     $this->halt("对不起,信息内容不能为空");
else
     $this->halt("发送失败,可能服务器忙,请稍候重试");
    }
    /* public */
     
    function Receive($SMobile,$DMobile,$Msg)
    {     
/*
Id:连接标识号,每一个Id对应一个服务号码和计费方式,一般情况下每客户端只有一个Id号。如果该客户
     端下接好多种服务,即可使用多Id的情况。
SMobile:呼叫方手机号码,即用户手机号码
DMobile:被叫服务号码.
Msg:短信内容。
*/
$this->RLogin();
  
$command = "Receive Id=".$this->Id."&pwd=".$this->Password;
$command .= "&SMobile=$SMobile&DMobile=$DMobile&Msg=$Msg";
$command .= "\r\n";
echo $command;
fputs($this->fp,$command);
$tempstr = fgets($this->fp,20);
if(ereg("OK",$tempstr))
     return ture;
else     
     $this->halt("注册失败,可能服务器忙,请稍候重试");
    }
     
    /* public: close the connection*/
    function close()
    {
if($this->fp)
     fclose($this->fp);
    }
     
    /* private */
    function halt($msg)
    {
$this->close();
exit($msg);
    }
}
?>


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/13004/showart_68710.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP