?? mpi.inc.php
字號:
<?php/* * @FileName: mpi.inc.php * @Description: Class PushInitiator,通過MISC來發送PUSH的PI。完成推送消息的創建以及發送 * @Functions: * PushInitiator - 構造函數 * send_push_message - 發送指定類型的消息到指定的地址,需要指定消息內容及其類型 * send_si_message - 發送SI消息到指定的地址。實際上是調用send_push_message來發送SI消息。 * create_pap_message - 生成一個PAP控制消息 * create_si_message - 生成一個SI消息 * get_push_id - 生成一個新的push id,根據當前時間及一個隨機數 * set_push_response - 設置push response消息到類成員變量 * get_push_response - 輸出push response消息 * set_error - 設置類成員錯誤變量值 * get_last_error - 輸出最近的錯誤消息。 * * * @Author: Hydra@G.Feel * @Version: 1.0/2004-6-30 13:44 * @Reference: * 1.WAP Push Architectural Overview,WAP FORUM,WAP-250-PushArchOverview-20010703-a * 2.Push Access Protocol, WAP FORUM,WAP-247-PAP-20010429-a * 3.Service Indication,WAP FORUM,WAP-167-ServiceInd-20010731-a * 4. WAP PUSH SP接口協議, 中國移動通信 * @Usage Sample: * $pi = new PushInitiator(); * if( !$pi->send_si_message("13910845314","http://wap.goodfeel.com.cn/","Welcome to GoodFeel!") ) * echo $pi->get_last_error(); * * $Id: pi.inc.php,v1.0 2003-4-21 11:36 Exp$ */include("mpi.h.php");class PushInitiator{ // PPG related global variables //var $_ppg_ip = "211.136.16.39"; //Beijing Gateway var $_ppg_ip = "211.136.22.55"; //Wuhan Gateway var $_ppg_port = 5080; var $_ppg_url = "/PGW"; // content type for the push request body part var $_content_type = "text/plain"; // source-reference var $_pi_mark = "GoodFeel Push Initiator v1.0"; //與PUSH有關的一些變量 var $_deliver_before = ""; var $_deliver_after = ""; var $_si_expires = ""; var $_content_encoding = MPI_ENC_UTF8; var $_message_id = ""; // response from the Push Proxy Gateway var $_push_response; // error handling variables var $_errno = 0; var $_errstr = ""; // debug mode switch var $_debug = false; /* Function: public PushInitiator * ------------------------------------------- * Purpose: Constructor Function * Arguments: $ppg_ip - Push Proxy Gateway IP address * $ppg_port - Push Proxy Gateway Server port * $ppg_url _ Push Proxy Gateway Server url * Returns: void * Comments: * History: */ function PushInitiator() { $numargs = func_num_args(); switch($numargs) { case 0: //Using default variables break; case 1: //the argument is a http url including ip,port,uri $url_array = parse_url(func_get_arg(0)); $this->_ppg_ip = $url_array["host"]; if( $url_array["port"] == 0 ) $url_array["port"] = 80; $this->_ppg_port = $url_array["port"]; $this->_ppg_url = $url_array["path"]; break; case 3: //the 3 arguments are ppg_ip,ppg_port,ppg_url $ppg_ip = func_get_arg(0); $ppg_port = func_get_arg(1); $ppg_url = func_get_arg(2); if($ppg_ip != "") $this->_ppg_ip = $ppg_ip; if(intval($ppg_port) != 0) $this->_ppg_port = $ppg_port; if($ppg_url != "") $this->_ppg_url = $ppg_url; break; default: //using default variables break; } } /* Function: public send_push_message * ------------------------------------------- * Purpose: send specified message to specified address, including push message & * content with its mime type. * Arguments: $address - string,the address the push msg is sent to. * could be: Device addresses as IP or MSISDN * or User-defined identifiers * $mime_type - string, content mime type, i.e. text/vnd.wap.si * $message - string, message content, such as si message * $address_type - string, client address type * could be: USER,PLMN,IPv4,IPv6 * Returns: on error, return FALSE, on success ,return push id. * Comments: * $address和$address_type必須一一對應。MSISDN--PLMN,IP--IPv4 * History: */ function send_push_message($message) { $fp = fsockopen($this->_ppg_ip, $this->_ppg_port); if( $fp ) { //生成POST到PPG的內容 $postcontent = $message; //HTTP 頭部信息 $httpstr = "POST $this->_ppg_url HTTP/1.1\r\n"; $httpstr.= "Content-Type: " . $this->_content_type . "\r\n"; $httpstr.= "Host: " . $this->_ppg_ip . ":" . $this->_ppg_port. "\r\n"; $httpstr.= "User-Agent: " . $this->_pi_mark . "\r\n"; $httpstr.= "Content-Length: ".strlen($postcontent)."\r\n\r\n"; //debug info if( $this->_debug) { $fp_log = @fopen("/usr/local/system/log/mpi.log","a+"); @fwrite($fp_log,$httpstr . "\n"); @fwrite($fp_log,$postcontent . "\n"); @ ($fp_log); } fputs($fp,$httpstr. $postcontent. "\r\n\r\n");//{ -------------------- Modified By JH.Zhang@G.Feel at 2003-5-27 如果存在 $_PUSH_NO_RESPOND 全局變量,不處理響應 if( $GLOBALS["_PUSH_NO_RESPOND"] ) { return true; }//} -------------------- Modified By JH.Zhang@G.Feel at 2003-5-27 $strget = ""; /* while(!feof($fp)) { $strget .= fgets($fp,4096); } fclose($fp); //$strget = strtolower($strget); //處理PPG端的響應。 $response = explode("\r\n",$strget); $http_code = explode(" ",$response[0]); $code = intval($http_code[1]); switch( $code ) { case 202: foreach($response as $element) { if( strpos($element,"<?xml version") === false ) { } else { $this->set_push_response($element); break; } } break; default: $this->set_error(-2,$strget); return FALSE; break; } */ // 設置讀取超時時間 stream_set_timeout($fp, 5); // 讀 HTTP 頭部信息 while(!feof($fp)) { $ls_Str = fgets($fp,1024); if( !trim($ls_Str) ) { break; } $pos = strpos( $ls_Str, ": " ); $sso_headers[ substr($ls_Str,0,$pos) ] = substr($ls_Str,$pos+2); $stream_status = stream_get_meta_data($fp); if( $stream_status["timed_out"]==true ) { $other_error_info = join( "\n", $sso_headers ); $this->set_error(-1,"Time out while waiting response-HTTP HEADER"); return false; } } // 按長度讀取 if( $sso_headers["Content-Length"] ) { $strget = fread( $fp, intval($sso_headers["Content-Length"]) ); $stream_status = stream_get_meta_data($fp); if( $stream_status["timed_out"]==true ) { $other_error_info = $strget; $this->set_error(-1,"Time out while waiting response-HTTP Entity"); return false; } } // 一個字節一個字節的讀 else { while(!feof($fp)) { $ls_Str = fread($fp,1); $strget .= $ls_Str; if( strstr($strget,"</misc_command>") ) { break; } $stream_status = stream_get_meta_data($fp); if( $stream_status["timed_out"]==true ) { $other_error_info = $strget; $this->set_error(-1,"Time out while waiting response-HTTP Entity"); return false; } } } fclose($fp); $this->set_push_response($strget); if( $this->_debug) { $fp_log = @fopen("/usr/local/system/log/mpi.log","a+"); @fwrite($fp_log,$strget . "\n"); @fclose($fp_log); } return true; } else { $this->set_error(-1,"Error in open http connection"); return FALSE; } } /* Function: public send_si_message * ------------------------------------------- * Purpose: send Service Indication message to specified address, * Arguments: $address - string,the address the push msg is sent to. * could be: Device addresses as IP or MSISDN * or User-defined identifiers * $url - string,the url is used to access the content * $message - string, the message displayed to the user * $sid - string, identify the SIs * $created - string, the time that content is created, NOT SI * the format is YYYY-MM-DDThh:mm:ssZ * Returns: on error, return -1, on success ,return push id. * Comments: * $address和$address_type必須一一對應。MSISDN--PLMN,IP--IPv4 * History: */ function send_si_message($service_id,$url,$content,$id,$notify_to,$to,$from="") { $message_id = $this->create_push_id($id); $si_message = $this->create_si_message($service_id,$url,$content,$message_id,$notify_to,$to,$from=""); if( $this->send_push_message($si_message) ) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -