1. 程式人生 > >之前寫的CMPP3.0未完成程式碼

之前寫的CMPP3.0未完成程式碼

如題,參考大佬部落格寫的代http://blog.csdn.net/blin911/article/details/54344609,我自個因公司決策沒繼續往下寫了,貼出來做分享參考

class Cmpp {
    // 設定項
    public $host = ;
    public $port = ;
    public $Source_Addr = ;
    public $Shared_secret = ;
    public $SP_ID = ;
    public $SP_CODE = ;
    public $Service_Id = ;

    private
$socket; private $Sequence_Id = 0; private $bodyData; private $AuthenticatorSource; public function createSocket(){ $socket = fsockopen( $this->host, $this->port, $errno, $errstr, 20 ); if(!$socket) $this
->throwErr("can't create socket .", __LINE__); socket_set_timeout($socket, 1200); $this->socket = $socket; } public function CMPP_CONNECT(){ $Source_Addr = $this->Source_Addr; $Version = 0x30; $Timestamp = date('mdHis'); $AuthenticatorSource
= $this->createAS($Timestamp); // 06 Octet String Source_Addr // 16 Octet String AuthenticatorSource // 01 Unsigned Integer Version // 04 Unsigned Integer Timestamp $bodyData = pack("a6a16CN", $Source_Addr, $AuthenticatorSource, $Version, $Timestamp); $this->AuthenticatorSource = $AuthenticatorSource; $this->send($bodyData, 0x00000001); } public function CMPP_CONNECT_RESP(){ // 04 Unsigned Integer Status // 16 Octet String AuthenticatorISMG // 01 Unsigned Integer Version $body = unpack("NStatus/a16AuthenticatorISMG/CVersion", $this->bodyData); // TODO ISMG認證失敗,檢查原因 // $this->cheakAISMG($body['Status'], $body['AuthenticatorISMG']); $this->Sequence_Id = $this->Sequence_Id + 1; } public function CMPP_SUBMIT($to, $text){ // 08 Unsigned Integer Msg_Id // 01 Unsigned Integer Pk_total // 01 Unsigned Integer Pk_number // 01 Unsigned Integer Registered_Delivery // 01 Unsigned Integer Msg_level // 10 Octet String Service_Id // 01 Unsigned Integer Fee_UserType // 32 Octet String Fee_terminal_Id // 01 Unsigned Integer Fee_terminal_type // 01 Unsigned Integer TP_pId // 01 Unsigned Integer TP_udhi // 01 Unsigned Integer Msg_Fmt // 06 Octet String Msg_src // 02 Octet String FeeType // 06 Octet String FeeCode // 17 Octet String ValId_Time // 17 Octet String At_Time // 21 Octet String Src_Id // 01 Unsigned Integer DestUsr_tl // * Octet String Dest_terminal_Id // 01 Unsigned Integer Dest_terminal_type // 01 Unsigned Integer Msg_Length // * Octet String Msg_Content $Msg_Id = 0; $Pk_total = 1; $Pk_number = 1; $Registered_Delivery = 0; $Msg_level = 0; $Service_Id = $this->Service_Id; $Fee_UserType = 2; $Fee_terminal_Id = ''; $Fee_terminal_type = 0; $TP_pId = 0; $TP_udhi = 0; $Msg_Fmt = 0; $Msg_src = $this->SP_ID; $FeeType = 03; $FeeCode = ""; $ValId_Time = ""; $At_Time = ""; $Src_Id = $this->SP_CODE; $DestUsr_tl = 1; $Dest_terminal_Id = $to; $Dest_terminal_type = 1; $Msg_Length = strlen($text); $Msg_Content = $text; $bodyData = pack( "a8CCCCa10Ca32CCCCa6a2a6a17a17a21Ca32CCa".$Msg_Length, $Msg_Id, $Pk_total, $Pk_number, $Registered_Delivery, $Msg_level, $Service_Id, $Fee_UserType, $Fee_terminal_Id, $Fee_terminal_type, $TP_pId, $TP_udhi, $Msg_Fmt, $Msg_src, $FeeType, $FeeCode, $ValId_Time, $At_Time, $Src_Id, $DestUsr_tl, $Dest_terminal_Id, $Dest_terminal_type, $Msg_Length, $Msg_Content ); $this->log([ $Msg_Id, $Pk_total, $Pk_number, $Registered_Delivery, $Msg_level, $Service_Id, $Fee_UserType, $Fee_terminal_Id, $Fee_terminal_type, $TP_pId, $TP_udhi, $Msg_Fmt, $Msg_src, $FeeType, $FeeCode, $ValId_Time, $At_Time, $Src_Id, $DestUsr_tl, $Dest_terminal_Id, $Dest_terminal_type, $Msg_Length, $Msg_Content]); $this->log($bodyData); $this->send($bodyData, 0x00000004); } public function CMPP_SUBMIT_RESP(){ // 08 Unsigned Integer Msg_Id // 04 Unsigned Integer Result $body = unpack("N2Msg_Id/NResult", $this->bodyData); $this->debug($this->bodyData, 1); } public function send($bodyData, $Command_Id){ $Total_Length = strlen($bodyData) + 12; $Sequence_Id = $this->Sequence_Id; // 04 Unsigned Integer Total_Length // 04 Unsigned Integer Command_Id // 04 Unsigned Integer Sequence_Id $headData = pack("NNN", $Total_Length, $Command_Id, $Sequence_Id); // 傳送訊息 $this->log("send $Command_Id"); fwrite($this->socket, $headData.$bodyData, $Total_Length); $this->listen($Sequence_Id); } public function listen($Sequence_Id){ // do{ if(feof($this->socket)){ $this->throwErr("socket was close .", __LINE__); } // 處理頭 $headData = fread($this->socket, 12); if(empty($headData)){ $this->throwErr("can't get any data.", __LINE__); } $head = unpack("NTotal_Length/NCommand_Id/NSequence_Id", $headData); $this->log("get ".($head['Command_Id'] & 0x0fffffff)); // 處理body $this->bodyData = fread($this->socket, $head['Total_Length'] - 12); switch ( $head['Command_Id'] & 0x0fffffff ) { case 0x00000001: $this->CMPP_CONNECT_RESP(); break; case 0x00000004: $this->CMPP_SUBMIT_RESP(); break; default: $this->throwErr("unknow Command_Id .".$head['Command_Id'], __LINE__); break; } // } while ( $this->Sequence_Id != $Sequence_Id ); } /** * AuthenticatorSource = MD5(Source_Addr+9 位元組的0 +shared secret+timestamp) * @param [type] $Timestamp [description] * @return [type] [description] */ public function createAS($Timestamp){ $temp = $this->Source_Addr . pack("a9","") . $this->Shared_secret . $Timestamp; return md5($temp, true); } /** * AuthenticatorISMG =MD5(Status + AuthenticatorSource + shared secret) * @param [type] $Status [description] * @param [type] $AuthenticatorISMG [description] * @return [type] [description] */ public function cheakAISMG($Status, $AuthenticatorISMG){ $temp = $Status . $this->AuthenticatorSource . $this->Shared_secret; $this->debug($temp.pack("a",""), 1, 1); $this->debug($AuthenticatorISMG.pack("a",""), 2, 1); if($AuthenticatorISMG != md5($temp, true)){ $this->throwErr("ISMG can't pass check .", __LINE__); } } public function log($data, $line = null){ if($line){ $data = $line . " : ".$data; } file_put_contents("./cmpp.log", print_r($data, true).PHP_EOL, FILE_APPEND); } public function debug($data, $fileName, $noExit = false){ file_put_contents("./$fileName.debug", print_r($data, true)); if(!$noExit) exit; } public function throwErr($info, $line){ die("info: $info in line :$line"); } } @unlink("./cmpp.log"); $cmpp = new Cmpp; $cmpp->createSocket(); $cmpp->CMPP_CONNECT(); // $cmpp->CMPP_SUBMIT("xxxxxxxxx", '123');

簡單說一下,因為時間太久了很多引數什麼的我也忘了,只記得是調通了握手那裡,其他的我也沒做,當時只給了兩天來著,程式碼應該也巨坑,我也沒搞定就交給java仁兄了