⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mpi.inc.php

📁 这个是发送网站push的php程序
💻 PHP
📖 第 1 页 / 共 2 页
字号:
<?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 + -