本帖最后由 ttm1984 于 2010-03-03 22:31:17 编辑

解决方案 »

  1.   

        /**
         *注册飞信号码并获取临时变量NONCE和SSIC
         **/
        private function FetionGetSIPCNonce(){
            $REQUEST_SIPC_SIGN_NONCE = sprintf($this->REQUEST_SIPC_SIGN_NONCE, $this->domain_fetion,
                                               $this->fetion_no, strlen($this->REQUEST_SIPC_SIGN_NONCE_BODY),
                                               $this->REQUEST_SIPC_SIGN_NONCE_BODY) ;
            $sock_data = socket_write($this->socket, $REQUEST_SIPC_SIGN_NONCE);
            $buf = '' ;
            if (false == ($buf = socket_read($this->socket, 10000))) {
                echo "Line:" . __LINE__ . "socket_read() failed; reason: " . socket_strerror(socket_last_error($this->socket)) . "\n";
            }
            $regex_ssic = '/.*nonce=\"(\\w+)\".*/s' ;
            if(!preg_match($regex_ssic, $buf, $matches)){
                echo "Fetion Error: No nonce found in socket\n";
            }
            $this->NONCE = strtoupper(trim($matches[1]));        $regex_ssic = '/ssic\s+(.*)/s';
            if (!preg_match($regex_ssic, file_get_contents($this->cookie_file), $matches)) {
                echo "Fetion Error: No ssic found in cookie\n";
            }
            echo $this->SSIC = trim($matches[1]);
        }
        
        /**
         *登录飞信服务器
         **/
        private function FetionLogin(){
            $this->RESPONSE = $this->FetionEncryptPassWD() ;
            $REQUEST_SIPC_LOGIN = sprintf($this->REQUEST_SIPC_LOGIN,
                                          $this->domain_fetion, $this->fetion_no,
                                          $this->RESPONSE, $this->C_NONCE,
                                          strlen($this->REQUEST_SIPC_LOGIN_BODY),
                                          $this->REQUEST_SIPC_LOGIN_BODY);
            $buf = '' ;
            $sock_data = socket_write($this->socket, $REQUEST_SIPC_LOGIN);
            if (false == ($buf = socket_read($this->socket, 10000))) {
                echo "Line:" . __LINE__ . "socket_read() failed; reason: " . socket_strerror(socket_last_error($this->socket)) . "\n";
            }
    //echo $buf,"\n";
            if(preg_match_all('/200/s', $buf, $matches)){
                return True;
            }else{
                return False;
            }
        }

        
    /**
     * 获取飞信用户列表
     * */
    private function FetionGetContactLists(){
    echo $REQUEST_GETCONTACTLISTS = sprintf($this->REQUEST_SIPC_GetContactList,
    $this->domain_fetion,
    $this->fetion_no,
    strlen($this->REQUEST_SIPC_GetContactListBody),
    $this->REQUEST_SIPC_GetContactListBody);
    $buf = '' ;
    $sock_data = socket_write($this->socket, $REQUEST_GETCONTACTLISTS);
    if (false == ($buf = socket_read($this->socket, 10000))) {
    echo "\nLine:" . __LINE__ . " socket_read() failed; reason: " . socket_strerror(socket_last_error($this->socket)) . "\n";
    }
    //echo $buf,"\n\n\n";
    file_put_contents("ContactList.txt",$buf);
    if(preg_match_all('/(sip:\\d+@fetion\.com\.cn;p=\\d+)/s',$buf,$matches)){
    $fetion_ContactList = $matches[1];
    foreach($fetion_ContactList as $sip){
    $this->fetion_ContactList .= "<contact uri=\"$sip\" />";
    }
    return true;
    }
    return false;
    }


    /**
     * 获取用户信息,并建立手机号与SIP号映射关系
     *
     */
    private function FetionGetContactsInfo(){
    $this->REQUESR_SIPC_GetContactsInfo_body = sprintf($this->REQUESR_SIPC_GetContactsInfo_body, $this->fetion_ContactList);
    $REQUESR_SIPC_GetContactsInfo = sprintf($this->REQUESR_SIPC_GetContactsInfo,
    $this->domain_fetion,
    $this->fetion_no,
    strlen($this->REQUESR_SIPC_GetContactsInfo_body),
    $this->REQUESR_SIPC_GetContactsInfo_body
    );
    $sock_data = socket_write($this->socket, $REQUESR_SIPC_GetContactsInfo);
    if (false == ($buf = socket_read($this->socket, 1024*10))) {
    echo "\nLine:" . __LINE__ . " socket_read() failed; reason: " . socket_strerror(socket_last_error($this->socket)) . "\n";
    }
    //echo $buf,"\n\n";
    file_put_contents("GetContactsInfo.txt",$buf);
    if(preg_match_all('/<contact uri=\"(.*?)\".*?mobile\-no=\"(.*?)\".*?\/contact>/s',$buf,$matches)){
    $total = count($matches[1]);
    for($i=0; $i<$total; $i++){
    $uri = $matches[1][$i];
    $mobile = $matches[2][$i];
    $this->fetion_ContactMapping["$mobile"] = $uri ; // "13811111111" => "sip:[email protected];p=9272"
    }
    return true;
    }
    else{
    return false;
    }
    }
        /**
         *发短信
         **/
        public function FetionSendSMS($receiver_sip){
            //"M %s SIP-C/2.0\r\nF: %s\r\nI: 2\r\nQ: 1 M\r\nT: tel:%s\r\nN: SendSMS\r\nL: %d\r\n\r\n%s";
    // $receiver_mobile = $this->SMS_RECEIVER ;
    // if(!array_key_exists($receiver_mobile,$this->fetion_ContactMapping)) return false;
    // $receiver_sip = $this->fetion_ContactMapping[$receiver_mobile] ; // 转换手机号为"sip:[email protected];p=1212"格式
            $REQUEST_SENDSMS = sprintf($this->REQUEST_SIPC_SENDSMS,
                                       $this->domain_fetion, $this->fetion_no,
                                       // "tel:" . $this->SMS_RECEIVER, // "tel:13822222222" 方式目前已经不支持
                       $receiver_sip,  
                                       strlen($this->SMS_TEXT),
                                       $this->SMS_TEXT) ;
            $buf = '' ;
            $sock_data = socket_write($this->socket, $REQUEST_SENDSMS);
            if (false == ($buf = socket_read($this->socket, 1000))) {
                echo "\nLine:" . __LINE__ . " socket_read() failed; reason: " . socket_strerror(socket_last_error($this->socket)) . "\n";
            }
    //echo $buf,"\n";
            if(preg_match_all('/200/s', $buf, $matches)){
                return True;
            }else{
                return False;
            }
        }
        
        /**
         *登出飞信服务器
         **/
        private function FetionLogout(){
            //string Logout = String.Format(FETION_SIPC_LOGOUT, FETION_DOMAIN_URL, Fetion_Number);
            $FETION_SIPC_LOGOUT   = "R %s SIP-C/2.0\r\nF: %s\r\nI: 1 \r\nQ: 3 R\r\nX: 0\r\n\r\n";
            $REQUEST_SIPC_LOGOUT = sprintf($this->REQUEST_SIPC_LOGOUT,
                                           $this->domain_fetion,
                                           $this->fetion_no) ;
            @socket_write($this->socket, $REQUEST_SIPC_LOGOUT);
            socket_close($this->socket) ;
        }    /**
         *生成加密串,感谢CSND ycTIN的帮助!
         *@return string 加密的密码串     *
         **/
            
        private function FetionEncryptPassWD() {
            $key = md5($this->fetion_no . ':' . $this->domain_fetion . ':' . $this->fetion_pwd, true);
            $h1 = strtoupper(md5($key . ':' . $this->NONCE . ':' . $this->C_NONCE));
            
            $h2 = "REGISTER:" . $this->fetion_no ;
            $h2 = strtoupper(md5($h2));
            
            $response = "$h1:" . $this->NONCE . ":" . $h2;
            $response = strtoupper(md5($response));
            
            return $response ;
        }    /**
         *打印一下临时变量
         **/
        public function printVar(){
            print "\nCNONCE:" . $this->C_NONCE;
            print "\nDomain:" . $this->domain_fetion;
            print "\nNONCE:" . $this->NONCE;
            print "\nRESPONSE:" . $this->RESPONSE;
        }
        public function __destruct(){
            //$this->FetionLogout() ;
            @unlink($this->cookie_file); // 删除cookie文件
        }
    }$myNewFetion = new myFetion('13800000000', 'abcde12345', 'SELF', '给我自己发');
    $myNewFetion = new myFetion('13800000000', 'abcde12345', '13811111111,13822222222,13833333333', '大家注意了:这是一条PHP飞信短信发送类测试短信。如果你能收到,说明我的群发功能已经实现!收到请回复,Thanks!');
    $myNewFetion = new myFetion('13800000000', 'abcde12345', 'ALL', 'PHP短信拜年了!');
    //$myNewFetion->printVar() ;
    ?>
      

  2.   

    点击下载PHP短信发送类完整代码
      

  3.   

    我以前玩过php 通过飞信的接口来发送每天的天气预报
      

  4.   

    为什么会显示错误呢?
    Fatal error: Call to a member function getAttribute() on a non-object in C:\wamp\www\fetion\class.myfetion.php on line 196
      

  5.   

    显示错误好友还是发送不成功Fetion Error: No ssic found in cookie
      

  6.   

    221.176.31.4
    这个IP是移动官方的,还是第三方的?是否安全?private $SIPC_PROXY         = '221.176.31.4:8080';  //  8080端口飞信通讯使用
      

  7.   

    8080端口是飞信服务器的端口。
    IP地址是从导航网站的XML中获取的,非第三方服务器。代码里也没有直接使用这个IP。
    开源就是为了安全,请放心使用。
      

  8.   

    不知道为什么 那个测试页 会弄出来 http 500 错误
    地址:http://m.jianhome.cn/f/fetion.php检查空间 有 2个xml 能成功生成 不知道 哪里出的问题
      

  9.   

    请各位重新下载测试程序,我做了更新。http://i.isclab.org/tools/fetion.zip *@author shadu AT foxmail DOT com
     *@copyright 任意拷贝和修改!
     *@version 2010-03-22
     *@update 采用联系方式缓存技术,加速短信发送速度(程序所在目录需要给写权限,第一次使用该程序可能有点慢)
     * ----
     *@version 2010-03-16
     *@update 失败后重新登录(最多三次),减少了漏发现象。现在几乎没有了。
     *@update 设置超时等待时间为2秒,减少了白屏现象
     *@update 对socket做了优化,联系人较多的时候也能处理
     *@update 使用fsockopen方式发短息,取消了原来直接使用socket模块的方式。有些虚拟主机禁用了socket函数。
     *@update 给自己发短信可以不用SELF了,直接输入手机号码
      

  10.   

    Fatal error: Call to a member function getAttribute() on a non-object in D:\Software\PHPnow\htdocs\fetion\class.myfetionV5.php  on line 224一运行就这样,是怎么回事呢?
      

  11.   

        private function FetionGetContactsInfo(){
            $this->REQUESR_SIPC_GetContactsInfo_body = sprintf($this->REQUESR_SIPC_GetContactsInfo_body, $this->fetion_ContactList);
            $REQUESR_SIPC_GetContactsInfo = sprintf($this->REQUESR_SIPC_GetContactsInfo,
                    $this->domain_fetion,
                    $this->fetion_no,
                    strlen($this->REQUESR_SIPC_GetContactsInfo_body),
                    $this->REQUESR_SIPC_GetContactsInfo_body
                    );
            $sock_data = socket_write($this->socket, $REQUESR_SIPC_GetContactsInfo);
            if (false == ($buf = socket_read($this->socket, 1024*10))) {
                echo "\nLine:" . __LINE__ . " socket_read() failed; reason: " . socket_strerror(socket_last_error($this->socket)) . "\n";
            }
            //echo $buf,"\n\n";
            file_put_contents("GetContactsInfo.txt",$buf);
            if(preg_match_all('/<contact uri=\"(.*?)\".*?mobile\-no=\"(.*?)\".*?\/contact>/s',$buf,$matches)){
                $total = count($matches[1]);
                for($i=0; $i<$total; $i++){
                    $uri = $matches[1][$i];
                    $mobile = $matches[2][$i];
                    $this->fetion_ContactMapping["$mobile"] = $uri ; // "13811111111" => "sip:[email protected];p=9272"
                }
                return true;
            }
            else{
                return false;
            }
        }这个函数能否修改成curl版的?
    自己修改了,但是测试失败。。取到的内容为空
      

  12.   

    Fatal error: Call to a member function getAttribute() on a non-object in D:\Apache2.2\htdocs\topkey_admin\fetion\class.myfetionV5.php on line 222一測試就出現這個問題是怎麼回事呀?
      

  13.   

    前几个都用的好好的,最近无法正常使用了,一直报错:
    PHP Fatal error:  Call to a member function getAttribute() on a non-object in C:\Inetpub\SolarWinds\fetion\class.myfetionV5.php on line 222看楼上有人遇到同样的问题,说是密码输错了,但我确信密码是正确的,不知道该怎么解决?
      

  14.   

    test4.xml中数据是啥?给个完整的
    谢谢
      

  15.   

    抱歉,各位!
    我很久没来CSDN了,密码也差点忘了。我是自己锻炼和使用,才写了这个程序。因为工作较忙,加上飞信接口升级,目前这个程序已经不能正常工作,我也没有时间维护。
    再次感谢大家!