zoukankan      html  css  js  c++  java
  • 推送

    <?php
    //php需要开启ssl(OpenSSL)支持
    $apnsCert = "ck.pem";//连接到APNS时的证书许可文件,证书需格外按要求创建
    $pass = "123456";//证书口令
    $serverUrl = "ssl://gateway.sandbox.push.apple.com:2195";//push服务器,这里是开发测试服务器
    $deviceToken = "a8fcd4aa8943b223d4ebcd54fe168a8b99b3f24c63dbc0612db25a8c0a588675";//ios设备id,中间不能有空格,每个ios设备一个id
    $message = $_GET ['message'] or $message = "hello!";
    $badge = ( int ) $_GET ['badge'] or $badge = 2;
    $sound = $_GET ['sound'] or $sound = "default";
    $body = array('aps' => array('alert' => $message , 'badge' => $badge , 'sound' => $sound));
    $streamContext = stream_context_create();
    stream_context_set_option ( $streamContext, 'ssl', 'local_cert', $apnsCert );
    stream_context_set_option ( $streamContext, 'ssl', 'passphrase', $pass );
    $apns = stream_socket_client ( $serverUrl, $error, $errorString, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $streamContext);//连接服务器
    if ($apns) {
    echo "Connection OK <br/>";
    } else {
    echo "Failed to connect $errorString";
    return;
    }
    $payload = json_encode ( $body );
    $msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack('n', strlen($payload)) . $payload;
    $result = fwrite ( $apns, $msg);//发送消息
    fclose ( $apns );
    if ($result)
    echo "Sending message successfully: " . $payload;
    else
    echo 'Message not delivered';
    ?>
    php做推送服务端实现android消息推送
    分类: php 2012-11-20 12:46 1058人阅读 评论(1) 收藏 举报
    
    下载tokudu-PhpMQTTClient-ba4e494.zip
    
    下载rsmb_1.2.0.zip(windows环境下要开windowsroker.exe   切记切记)
    
     
    
    解压tokudu-PhpMQTTClient-ba4e494.zip
    
    www下边新建目录androidpush
    
    把解压文件tokudu-PhpMQTTClient-ba4e494中的SAM文件夹copy到androidpush目录下
    
    把解压文件tokudu-PhpMQTTClient-ba4e494中的send_mqtt.php文件copy到androidpush目录下
    
    结构如图:
    
     
    
    send_mqtt.php主文件
    [php] view plaincopy
    
        <?php  
          
        require('SAM/php_sam.php');  
          
        //create a new connection object  
        //创建一个新的连接对象  
        $conn = new SAMConnection();  
          
        //start initialise the connection  
        //开始初始化连接 SAM_HOST服务器host路径 ;SAM_PORT服务器port端口号  
        $conn->connect(SAM_MQTT, array(SAM_HOST => '127.0.0.1',  
                                       SAM_PORT => 1883));        
        //create a new MQTT message with the output of the shell command as the body  
        //建立一个新的MQTT shell命令的输出消息以作为主体($msgCpu是通知内容)   
        //new SAMMessage() 参数一般写为json格式  
        $msgCpu = new SAMMessage("测试通知");  
          
        //send the message on the topic cpu  
        //发送该信息的主体  
        //860173018344139是设备号,每一台android手机对应一个唯一的设备号   
        //$msgCpu通知内容  
        //send()推送通知  
        $conn->send('topic://'.'860173018344139', $msgCpu);  
          
        //关闭连接  
        $conn->disconnect();           
          
        echo 'MQTT Message to ' . '860173018344139' . ' sent: ' . '测试通知';   
          
        ?>  
    
    
    SAM目录
    
    SAM/MQTT/sam_mqtt.php
    [php] view plaincopy
    
        <?php  
        /* 
        +----------------------------------------------------------------------+ 
        | Copyright IBM Corporation 2006, 2007.                                      | 
        | All Rights Reserved.                                                 | 
        +----------------------------------------------------------------------+ 
        |                                                                      | 
        | Licensed under the Apache License, Version 2.0 (the "License"); you  | 
        | may not use this file except in compliance with the License. You may | 
        | obtain a copy of the License at                                      | 
        | http://www.apache.org/licenses/LICENSE-2.0                           | 
        |                                                                      | 
        | Unless required by applicable law or agreed to in writing, software  | 
        | distributed under the License is distributed on an "AS IS" BASIS,    | 
        | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      | 
        | implied. See the License for the specific language governing         | 
        | permissions and limitations under the License.                       | 
        +----------------------------------------------------------------------+ 
        | Author: Dave Renshaw                                                 | 
        +----------------------------------------------------------------------+ 
         
        $Id: sam_mqtt.php,v 1.1 2007/02/02 15:36:46 dsr Exp $ 
         
        */  
          
        define("SAM_MQTT_CLEANSTART", "SAM_MQTT_CLEANSTART");  
        define("SAM_MQTT_QOS", "SAM_MQTT_QOS");  
        define("SAM_MQTT_SUB_SEPARATOR", "#-#");  
        /* --------------------------------- 
            SAMConnection 
           --------------------------------- */  
          
        class SAMConnection_MQTT {  
          
          var $debug = false;  
          
          var $errno = 0;  
          var $error = '';  
          
          /* 
           Info we need to keep between calls... 
          */  
          var $sub_id = '';  
          var $port = '';  
          var $host = '';  
          var $cleanstart = false;  
          var $virtualConnected = false;  
          var $connected = false;  
          /* 
           Our current open socket... 
          */  
          var $sock;  
          
          /* 
           Table of available operations using the MQTT protocol... 
          */  
          var $operations = array("MQTT_CONNECT"     => 1,  
                                  "MQTT_CONNACK"     => 2,  
                                  "MQTT_PUBLISH"     => 3,  
                                  "MQTT_PUBACK"      => 4,  
                                  "MQTT_PUBREC"      => 5,  
                                  "MQTT_PUBREL"      => 6,  
                                  "MQTT_PUBCOMP"     => 7,  
                                  "MQTT_SUBSCRIBE"   => 8,  
                                  "MQTT_SUBACK"      => 9,  
                                  "MQTT_UNSUBSCRIBE" => 10,  
                                  "MQTT_UNSUBACK"    => 11,  
                                  "MQTT_PINGREC"     => 12,  
                                  "MQTT_PINGRESP"    => 13,  
                                  "MQTT_DISCONNECT"  => 14);  
          
          /* --------------------------------- 
              Constructor 
             --------------------------------- */  
          function SAMConnection_MQTT() {  
            if ($this->debug) e('SAMConnection_MQTT()');  
          
            if ($this->debug) x('SAMConnection_MQTT()');  
          }  
          
          /* --------------------------------- 
              Commit 
             --------------------------------- */  
          function Commit() {  
            if ($this->debug) e('SAMConnection_MQTT.Commit()');  
          
            $errno = 100;  
            $error = 'Unsupported operation for MQTT protocol!';  
            $rc = false;  
          
            if ($this->debug) x("SAMConnection_MQTT.Commit() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              Connect 
             --------------------------------- */  
          function Connect($proto, $options=array()) {  
            if ($this->debug) e('SAMConnection_MQTT.Connect()');  
          
            /* Check our optional parameter array for the necessary bits...   */  
            if ($options[SAM_PORT] == '') {  
                $this->port = 1883;  
            } else {  
                $this->port = $options[SAM_PORT];  
            }  
            if ($options[SAM_HOST] == '') {  
                $this->host = 'localhost';  
            } else {  
                $this->host = $options[SAM_HOST];  
            }  
          
            $this->cleanstart = in_array(SAM_MQTT_CLEANSTART, $options);  
          
            if ($this->debug) t("SAMConnection_MQTT.Connect() host=$this->host, port=$this->port, cleanstart=$this->cleanstart");  
          
            if ($this->checkHost($this->host, $this->port)) {  
                $this->virtualConnected = true;  
            } else {  
                $this->virtualConnected = false;  
            }  
          
            if ($this->debug) x("SAMConnection_MQTT.Connect() rc=$this->virtualConnected");  
            return $this->virtualConnected;  
          }  
          
          /* --------------------------------- 
              Disconnect 
             --------------------------------- */  
          function Disconnect() {  
            if ($this->debug) e('SAMConnection_MQTT.Disconnect()');  
            $rc = false;  
          
            if ($this->virtualConnected) {  
                if ($this->connected) {  
                    $msg = $this->fixed_header("MQTT_DISCONNECT").pack('C', 0);  
                    fwrite($this->sock, $msg);  
                    $response = fgets($this->sock, 128);  
                    if ($this->debug) t('SAMConnection_MQTT.Disconnect() response is '.strlen($response).' bytes');  
                    if (strlen($response) == 0) {  
                        fclose($this->sock);  
                        $this->sock = NULL;  
                    }  
                }  
                $this->virtualConnected = false;  
                $this->connected = false;  
                $rc = true;  
            }  
          
            if ($this->debug) x("SAMConnection_MQTT.Disconnect() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              IsConnected 
             --------------------------------- */  
          function IsConnected() {  
            if ($this->debug) e('SAMConnection_MQTT.IsConnected()');  
            $rc = false;  
          
            if ($this->connected) {  
                $rc = true;  
            }  
          
            if ($this->debug) x("SAMConnection_MQTT.IsConnected() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              Peek 
             --------------------------------- */  
          function Peek() {  
            if ($this->debug) e('SAMConnection_MQTT.Peek()');  
          
            $errno = 100;  
            $error = 'Unsupported operation for MQTT protocol!';  
            $rc = false;  
          
            if ($this->debug) x("SAMConnection_MQTT.Peek() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              PeekAll 
             --------------------------------- */  
          function PeekAll() {  
            if ($this->debug) e('SAMConnection_MQTT.PeekAll()');  
          
            $errno = 100;  
            $error = 'Unsupported operation for MQTT protocol!';  
            $rc = false;  
          
            if ($this->debug) x("SAMConnection_MQTT.PeekAll() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              Receive 
             --------------------------------- */  
          function Receive($sub_id, $options=array()) {  
            if ($this->debug) e('SAMConnection_MQTT.Receive()');  
            $rc = false;  
          
            /* strip the topic from the rear of the subscription id...  */  
            $x = strpos($sub_id, SAM_MQTT_SUB_SEPARATOR);  
            if (!$x) {  
                $this->errno = 279;  
                $this->error = 'Specified subscription id ('.$sub_id.') is not valid!';  
                return false;  
            }  
            $topic = substr($sub_id, $x + strlen(SAM_MQTT_SUB_SEPARATOR));  
            $si = substr($sub_id, 0, $x);  
          
            /* Are we already connected?               */  
            if (!$this->connected) {  
                if ($this->debug) t('SAMConnection_MQTT.Receive() Not connected.');  
                /* No, so open up the connection...    */  
                $this->sub_id = $si;  
                $rc = $this->do_connect_now();  
            } else {  
                /* We are already connected. Are we using the right subscriber id?  */  
                if ($this->sub_id != $si) {  
                    if ($this->debug) t('SAMConnection_MQTT.Receive() Connected with wrong sub_id.');  
                    /* No, We better reconnect then...  */  
                    $this->disconnect();  
                    $this->sub_id = $si;  
                    $rc = $this->do_connect_now();  
                } else {  
                    if ($this->debug) t('SAMConnection_MQTT.Receive() Connected OK.');  
                    $rc = true;  
                }  
            }  
          
            if ($rc) {  
          
                /* have we got a timeout specified?    */  
                if ($options[SAM_WAIT] > 1) {  
                    $m = $options[SAM_WAIT] % 1000;  
                    $s = ($options[SAM_WAIT] - $m) /1000;  
                    if ($this->debug) t('SAMConnection_MQTT.Receive() timeout='.$options[SAM_WAIT]." ($s secs $m millisecs)");  
                    stream_set_timeout($this->sock, $s, $m);  
                    if ($this->debug) t('SAMConnection_MQTT.Receive() timeout set.');  
                } else {  
                    if ($this->debug) t('SAMConnection_MQTT.Receive() no timeout value found!');  
                }  
          
                $hdr = $this->read_fixed_header($this->sock);  
                if (!$hdr) {  
                    $this->errno = 500;  
                    $this->error = 'Receive request failed, timed out with no data!';  
                    $rc = false;  
                } else {  
                    if ($hdr['mtype'] == $this->operations['MQTT_PUBLISH']) {  
                        $len = $this->read_remaining_length($this->sock);  
                        if ($len > 1) {  
                            /* read the topic length...   */  
                            $topic = $this->read_topic($this->sock);  
                            if (!$topic) {  
                                $this->errno = 303;  
                                $this->error = 'Receive request failed, message format invalid!';  
                                $rc = false;  
                            } else {  
                                if ($this->debug) t('SAMConnection_MQTT.Receive() topic='.$topic);  
                                $len -= (strlen($topic) + 2);  
                                /* If QoS 1 or 2 then read the message id...   */  
                                if ($hdr['qos'] > 0) {  
                                    $idb = fread($this->sock, 2);  
                                    $len -= 2;  
                                    $fields = unpack('na', $idb);  
                                    $mid = $fields['a'];  
                                    if ($this->debug) t('SAMConnection_MQTT.Receive() mid='.$mid);  
                                }  
                                $payload = fread($this->sock, $len);  
                                if ($this->debug) t('SAMConnection_MQTT.Receive() payload='.$payload);  
                                $rc = new SAMMessage();  
                                $rc->body = $payload;  
                                $rc->header->SAM_MQTT_TOPIC = 'topic://'.$topic;  
                                $rc->header->SAM_MQTT_QOS = $hdr['qos'];  
                                $rc->header->SAM_TYPE = 'SAM_BYTES';  
                            }  
                        } else {  
                            $this->errno = 303;  
                            $this->error = 'Receive request failed, received message too short! No topic data';  
                            $rc = false;  
                        }  
                    } else {  
                        if ($this->debug) t('SAMConnection_MQTT.Receive() Receive failed response mtype = '.$mtype);  
                        $rc = false;  
                    }  
                }  
            }  
          
            if ($this->debug) x("SAMConnection_MQTT.Receive() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              Remove 
             --------------------------------- */  
          function Remove() {  
            if ($this->debug) e('SAMConnection_MQTT.Remove()');  
          
            $errno = 100;  
            $error = 'Unsupported operation for MQTT protocol!';  
            $rc = false;  
          
            if ($this->debug) x("SAMConnection_MQTT.Remove() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              Rollback 
             --------------------------------- */  
          function Rollback() {  
            if ($this->debug) e('SAMConnection_MQTT.Rollback()');  
          
            $errno = 100;  
            $error = 'Unsupported operation for MQTT protocol!';  
            $rc = false;  
          
            if ($this->debug) x("SAMConnection_MQTT.Rollback() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              Send 
             --------------------------------- */  
          function Send($topic, $message, $options=array()) {  
            if ($this->debug) e('SAMConnection_MQTT.Send()');  
            $rc = true;  
          
            /* check the format of the topic...   */  
            if (strncmp($topic, 'topic://', 8) == 0) {  
                $t = substr($topic, 8);  
            } else {  
                $this->errno = 279;  
                $this->error = 'Specified target ('.$topic.') is not a valid topic!';  
                return false;  
            }  
          
            if (in_array(SAM_MQTT_QOS, $options)) {  
                $qos = $options[SAM_MQTT_QOS];  
            } else {  
                $qos = 0;  
            }  
          
            /* Are we already connected?               */  
            if (!$this->connected) {  
                /* No, so open up the connection...    */  
                $this->do_connect_now();  
            }  
          
            $mid = rand();  
            $variable = $this->utf($t);  
            if ($qos > 0) {  
                $variable .= pack('n', $mid);  
            }  
          
            $payload = $message->body;  
          
            // add in the remaining length field and fix it together  
            $msg = $this->fixed_header("MQTT_PUBLISH", 0, $qos) . $this->remaining_length(strlen($variable)+strlen($payload)) . $variable . $payload;  
          
            fwrite($this->sock, $msg);  
            if ($qos > 0) {  
                $hdr = $this->read_fixed_header($this->sock);  
                if ($hdr) {  
                    /* is this a QoS level 1 message being sent?      */  
                    if ($qos == 1) {  
                        /* Yup, so we should get a PUBACK response message...    */  
                        if ($hdr['mtype'] == $this->operations['MQTT_PUBACK']) {  
                            $len = $this->read_remaining_length($this->sock);  
                            if ($len > 0) {  
                                $response = fread($this->sock, $len);  
                            }  
                            if ($len < 2) {  
                                if ($this->debug) t("SAMConnection_MQTT.Send() send failed, incorrect length response ($len) received!");  
                                $this->errno = 302;  
                                $this->error = 'Send request failed!';  
                                $rc = false;  
                            } else {  
                                $rc = true;  
                            }  
                        } else {  
                            if ($this->debug) t('SAMConnection_MQTT.Send() Send failed response mtype = '.$mtype.' Expected PUBREC!');  
                            $rc = false;  
                        }  
                    } else {  
                        /* lets assume it's QoS level 2...               */  
                        /* We should get a PUBREC response message...    */  
                        if ($hdr['mtype'] == $this->operations['MQTT_PUBREC']) {  
                            $len = $this->read_remaining_length($this->sock);  
                            if ($len > 0) {  
                                $response = fread($this->sock, $len);  
                            }  
                            if ($len < 2) {  
                                if ($this->debug) t("SAMConnection_MQTT.Send() send failed, incorrect length response ($len) received!");  
                                $this->errno = 302;  
                                $this->error = 'Send request failed!';  
                                $rc = false;  
                            } else {  
                                $rc = true;  
                                /* Now we can send a PUBREL message...       */  
                                $variable = pack('n', $mid);  
                                $msg = $this->fixed_header("MQTT_PUBREL").$this->remaining_length(strlen($variable)).$variable;  
                                fwrite($this->sock, $msg);  
          
                                /* get a response...                         */  
                                $hdr = $this->read_fixed_header($this->sock);  
                                if ($hdr['mtype'] == $this->operations['MQTT_PUBCOMP']) {  
                                    $len = $this->read_remaining_length($this->sock);  
                                    if ($len > 0) {  
                                        $response = fread($this->sock, $len);  
                                    }  
                                    if ($len < 2) {  
                                        if ($this->debug) t("SAMConnection_MQTT.Send() send failed, incorrect length response ($len) received!");  
                                        $this->errno = 302;  
                                        $this->error = 'Send request failed!';  
                                        $rc = false;  
                                    } else {  
                                        $rc = true;  
                                    }  
                                } else {  
                                    if ($this->debug) t('SAMConnection_MQTT.Send() Send failed response mtype = '.$mtype.' Expected PUBCOMP!');  
                                    $rc = false;  
                                }  
                            }  
                        } else {  
                            if ($this->debug) t('SAMConnection_MQTT.Send() Send failed response mtype = '.$mtype);  
                            $rc = false;  
                        }  
                    }  
                }  
            }  
          
            if ($this->debug) x("SAMConnection_MQTT.Send() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              SetDebug 
             --------------------------------- */  
          function SetDebug($option=false) {  
            $this->debug = $option;  
            return;  
          }  
          
          /* --------------------------------- 
              Subscribe 
             --------------------------------- */  
          function Subscribe($topic, $options=array()) {  
            if ($this->debug) e("SAMConnection_MQTT.Subscribe($topic)");  
            $rc = true;  
          
            /* check the format of the topic...   */  
            if (strncmp($topic, 'topic://', 8) == 0) {  
                $t = substr($topic, 8);  
            } else {  
                $this->errno = 279;  
                $this->error = 'Specified target ('.$topic.') is not a valid topic!';  
                return false;  
            }  
          
            if (in_array(SAM_MQTT_QOS, $options)) {  
                $qos = $options[SAM_MQTT_QOS];  
            } else {  
                $qos = 0;  
            }  
          
            /* Are we already connected?               */  
            if (!$this->connected) {  
                /* No, so open up the connection...    */  
                if (!$this->do_connect_now()) {  
                    return false;  
                }  
            }  
          
            // variable header: message id (16 bits)  
            $x = rand(1, 16000);  
            $variable = pack('n', $x);  
          
            // payload: client ID  
            $payload = $this->utf($t).pack('C', $qos);  
          
            // add in the remaining length field and fix it together  
            $msg = $this->fixed_header("MQTT_SUBSCRIBE", 0, 1) . $this->remaining_length(strlen($variable)+strlen($payload)) . $variable . $payload;  
          
            fwrite($this->sock, $msg);  
            $hdr = $this->read_fixed_header($this->sock);  
            if (!$hdr) {  
                if ($this->debug) t("SAMConnection_MQTT.Subscribe() subscribe failed, no response from broker!");  
                $this->errno = 301;  
                $this->error = 'Subscribe request failed, no response from broker!';  
                $rc = false;  
            } else {  
                if ($hdr['mtype'] == $this->operations['MQTT_SUBACK']) {  
                    $len = $this->read_remaining_length($this->sock);  
                    if ($len > 0) {  
                        $response = fread($this->sock, $len);  
                        /* Return the subscription id with the topic appended to it so we can unsubscribe easily... */  
                        $rc = $this->sub_id.SAM_MQTT_SUB_SEPARATOR.$t;  
                    }  
                    if ($len < 3) {  
                        if ($this->debug) t("SAMConnection_MQTT.Subscribe() subscribe failed, incorrect length response ($len) received!");  
                        $this->errno = 301;  
                        $this->error = 'Subscribe request failed, incorrect length response ($len) received!';  
                        $rc = false;  
                    }  
                } else {  
                    if ($this->debug) t('SAMConnection_MQTT.Subscribe() subscribe failed response mtype = '.$mtype);  
                    $rc = false;  
                }  
            }  
          
            if ($this->debug) x("SAMConnection_MQTT.Subscribe() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              Unsubscribe 
             --------------------------------- */  
          function Unsubscribe($sub_id) {  
            if ($this->debug) e("SAMConnection_MQTT.Unsubscribe($sub_id)");  
          
            /* Detach the topic from the rear of the subscription id...   */  
            $x = strpos($sub_id, SAM_MQTT_SUB_SEPARATOR);  
            if (!$x) {  
                $this->errno = 279;  
                $this->error = 'Specified subscription id ('.$sub_id.') is not valid!';  
                return false;  
            }  
          
            $topic = substr($sub_id, $x + strlen(SAM_MQTT_SUB_SEPARATOR));  
            $si = substr($sub_id, 0, $x);  
          
          
            /* Are we already connected?               */  
            if (!$this->connected) {  
                if ($this->debug) t('SAMConnection_MQTT.Unsubscribe() Not connected.');  
                /* No, so open up the connection...    */  
                $this->sub_id = $si;  
                $rc = $this->do_connect_now();  
            } else {  
                /* We are already connected. Are we using the right subscriber id?  */  
                if ($this->sub_id != $si) {  
                    if ($this->debug) t('SAMConnection_MQTT.Unsubscribe() Connected with wrong sub_id.');  
                    /* No, We better reconnect then...  */  
                    $this->disconnect();  
                    $this->sub_id = $si;  
                    $rc = $this->do_connect_now();  
                } else {  
                    if ($this->debug) t('SAMConnection_MQTT.Unsubscribe() Connected OK.');  
                    $rc = true;  
                }  
            }  
          
            /* variable header: message id (16 bits)  */  
            $x = rand(1, 16000);  
            $variable = pack('n', $x);  
          
            /* payload: client ID    */  
            $payload = $this->utf($topic);  
          
            /* add in the remaining length field and fix it together   */  
            $msg = $this->fixed_header("MQTT_UNSUBSCRIBE", 0, 1) . $this->remaining_length(strlen($variable)+strlen($payload)) . $variable . $payload;  
          
            fwrite($this->sock, $msg);  
            $hdr = $this->read_fixed_header($this->sock);  
            if (!$hdr) {  
                if ($this->debug) t("SAMConnection_MQTT.Unsubscribe() unsubscribe failed, no response from broker!");  
                $this->errno = 302;  
                $this->error = 'Unsubscribe request failed, no response from broker!';  
                $rc = false;  
            } else {  
                if ($hdr['mtype'] == $this->operations['MQTT_UNSUBACK']) {  
                    $len = $this->read_remaining_length($this->sock);  
                    if ($len > 0) {  
                        $response = fread($this->sock, $len);  
                        $rc = $this->sub_id;  
                    }  
                    if ($len != 2) {  
                        if ($this->debug) t("SAMConnection_MQTT.Unsubscribe() unsubscribe failed, incorrect length response ($len) received!");  
                        $this->errno = 301;  
                        $this->error = "Unsubscribe request failed, incorrect length response ($len) received!";  
                        $rc = false;  
                    }  
                } else {  
                    if ($this->debug) t('SAMConnection_MQTT.Unsubscribe() unsubscribe failed response mtype = '.$hdr['mtype']);  
                    $rc = false;  
                }  
            }  
          
            if ($this->debug) x("SAMConnection_MQTT.Unsubscribe() rc=$rc");  
            return $rc;  
          }  
          
          
          
          function remaining_length($l) {  
            /* return the remaining length field bytes for an integer input parameter   */  
            if ($this->debug) t("SAMConnection_MQTT.remaining_length() l=$l");  
          
            $rlf = '';  
            do {  
              $digit = $l % 128;  
              $l = ($l - $digit)/128;  
              if ($this->debug) t("SAMConnection_MQTT.remaining_length() digit=$digit l=$l");  
          
              # if there are more digits to encode, set the top bit of this digit  
              if ( $l > 0 ) {  
                $digit += 128;  
              }  
              $digit = pack('C', $digit);  
          
              $rlf .= $digit;  
              if ($this->debug) t("SAMConnection_MQTT.remaining_length() rlf=$rlf");  
            } while ($l > 0);  
          
            return $rlf;  
          }  
          
          function utf($s) {  
            /* return the UTF-8 encoded version of the parameter    */  
            $l = strlen($s);  
            $b1 = pack('C', $l/256);  
            $b2 = pack('C', $l%256);  
            $rc = $b1.$b2.$s;  
            return $rc;  
          }  
          
          function fixed_header($operation, $dup=0, $qos=0, $retain=0) {  
            /* fixed header: msg type (4) dup (1) qos (2) retain (1)   */  
            return pack('C', ($this->operations[$operation] * 16) + ($dup * 4) + ($qos * 2) + $retain);  
          }  
          
          function checkHost($hostname, $port) {  
              if ($this->debug) e("SAMConnection_MQTT.checkHost($hostname)");  
              $rc = false;  
          
              $fp = fsockopen($hostname, $port);  
              if (!$fp) {  
                  $rc = false;  
              } else {  
                  $this->sock = $fp;  
                  $rc = true;  
              }  
              if ($this->debug) x("SAMConnection_MQTT.checkHost(rc=$rc)");  
              return $rc;  
          }  
          
          function do_connect_now() {  
            $rc = true;  
          
            /* Do we have a client/subscriber id yet?       */  
            if ($this->sub_id == '') {  
                /* No, so create a unique one...            */  
                $this->sub_id = uniqid('', true);  
                if ($this->debug) t("SAMConnection_MQTT.do_connect_now() sub_id=$this->sub_id");  
            } else {  
                if ($this->debug) t("SAMConnection_MQTT.do_connect_now() using existing sub_id=$this->sub_id");  
            }  
          
            if ($this->cleanstart) {  
                $x = "x03";  
            } else {  
                $x = "x00";  
            }  
            $variable = $this->utf('MQIsdp')."x03$xx00x00";  
          
            /* payload is subscriber id                 */  
            $payload = $this->utf($this->sub_id);  
          
            /* add in the remaining length field and fix it together   */  
            $msg = $this->fixed_header("MQTT_CONNECT") . $this->remaining_length(strlen($variable)+strlen($payload)) . $variable . $payload;  
          
            $errno = 0;  
            $errstr = '';  
          
            if (!$this->virtualConnected) {  
                $fp = fsockopen($this->host, $this->port, $errno, $errstr);  
                if (!$fp) {  
                    if ($this->debug) t("SAMConnection_MQTT.do_connect_now() fsockopen failed! ($errno) $errstr");  
                    $this->errno = 208;  
                    $this->error = 'Unable to open socket to broker!';  
                    $this->sock = NULL;  
                    return false;  
                } else {  
                    $this->virtualConnected = true;  
                    $this->sock = $fp;  
                }  
            }  
          
            stream_set_timeout($this->sock, 10);  
            fwrite($this->sock, $msg);  
          
            $hdr = $this->read_fixed_header($this->sock);  
            if ($hdr) {  
                if ($hdr['mtype'] == $this->operations['MQTT_CONNACK']) {  
                    $len = $this->read_remaining_length($this->sock);  
                    if ($len < 2) {  
                        if ($this->debug) t("SAMConnection_MQTT.do_connect_now() connect failed, incorrect length response ($len) received!");  
                        $this->errno = 218;  
                        $this->error = 'Unable to open connection to broker!';  
                        $rc = false;  
                    } else {  
                        $response = fread($this->sock, $len);  
                        $fields = unpack('Ccomp/Cretcode', $response);  
                        if ($fields['retcode'] == 0) {  
                            $rc = $this->sock;  
                            $this->connected = true;  
                            $rc = true;  
                            if ($this->debug) t('SAMConnection_MQTT.do_connect_now() connected OK');  
                        } else {  
                            if ($this->debug) t('SAMConnection_MQTT.do_connect_now() connect failed retcode = '.$fields['retcode']);  
                            $rc = false;  
                            if ($fields['retcode'] == 2) {  
                                $this->sub_id = '';  
                                $this->errno = 279;  
                                $this->error = 'Invalid subscription id!';  
                            }  
                        }  
                    }  
                } else {  
                    if ($this->debug) t('SAMConnection_MQTT.do_connect_now() connect failed response mtype = '.$mtype);  
                    $rc = false;  
                }  
            }  
          
            if (!$rc) {  
                fclose($this->sock);  
                $this->sock = NULL;  
                $this->virtualConnected = false;  
            }  
          
            return $rc;  
          }  
          
          function read_fixed_header($conn) {  
              $rc = false;  
              $response = fread($conn, 1);  
              if (strlen($response) > 0) {  
                  $fields = unpack('Cbyte1', $response);  
                  $x = $fields['byte1'];  
                  $ret = $x % 2;  
                  $x -= $ret;  
                  $qos = ($x % 8) / 2;  
                  $x -= ($qos * 2);  
                  $dup = ($x % 16) / 8;  
                  $x -= ($dup * 8);  
                  $mtype = $x / 16;  
                  if ($this->debug) t("SAMConnection_MQTT.read_fixed_header() mtype=$mtype, dup=$dup, qos=$qos, retain=$ret");  
                  $rc = array('mtype' => $mtype, 'dup' => $dup, 'qos' => $qos, 'retain' => $ret);  
              }  
              return $rc;  
          }  
          
          function read_remaining_length($conn) {  
              $rc = 0;  
              $m = 1;  
              while (!feof($conn)) {  
                  $byte = fgetc($conn);  
                  $fields = unpack('Ca', $byte);  
                  $x = $fields['a'];  
                  if ($this->debug) t('SAMConnection_MQTT.read_remaining_length() byte ('.strlen($byte).') = '.$x);  
                  if ($x < 128) {  
                      $rc += $x * $m;  
                      break;  
                  } else {  
                      $rc += (($x - 128) * $m);  
                  }  
                  $m *= 128;  
              }  
              if ($this->debug) t('SAMConnection_MQTT.read_remaining_length() remaining length = '.$rc);  
              return $rc;  
          }  
          
          function read_topic($conn) {  
              if ($this->debug) e('SAMConnection_MQTT.read_topic()');  
              $rc = false;  
              while (!feof($conn)) {  
                  $tlen = fread($conn, 2);  
                  $fields = unpack('na', $tlen);  
                  if ($this->debug) t('SAMConnection_MQTT.read_topic() topic length='.$fields['a']);  
                  $rc = fread($conn, $fields['a']);  
                  break;  
              }  
              if ($this->debug) x("SAMConnection_MQTT.read_topic(rc=$rc)");  
              return $rc;  
          }  
        }  
          
        ?>  
    
    
     
    
    SAM/php_sam.php
    [php] view plaincopy
    
        <?php  
        /* 
        +----------------------------------------------------------------------+ 
        | Copyright IBM Corporation 2006, 2007.                                      | 
        | All Rights Reserved.                                                 | 
        +----------------------------------------------------------------------+ 
        |                                                                      | 
        | Licensed under the Apache License, Version 2.0 (the "License"); you  | 
        | may not use this file except in compliance with the License. You may | 
        | obtain a copy of the License at                                      | 
        | http://www.apache.org/licenses/LICENSE-2.0                           | 
        |                                                                      | 
        | Unless required by applicable law or agreed to in writing, software  | 
        | distributed under the License is distributed on an "AS IS" BASIS,    | 
        | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      | 
        | implied. See the License for the specific language governing         | 
        | permissions and limitations under the License.                       | 
        +----------------------------------------------------------------------+ 
        | Author: Dave Renshaw                                                 | 
        +----------------------------------------------------------------------+ 
         
        $Id: php_sam.php,v 1.1 2007/02/02 15:38:53 dsr Exp $ 
         
        */  
          
        /* Debugging flags and functions available to sub packages...   */  
        $eol = "
    ";  
        if (isset($_SERVER['REQUEST_URI'])) {  
            $eol = '<br>';  
        }  
        function e($s) {global $eol;echo '-->'.$s."$eol";}  
        function t($s) {global $eol;echo '   '.$s."$eol";}  
        function x($s) {global $eol;echo '<--'.$s."$eol";}  
          
        define('SAM_MQTT', 'mqtt');  
          
        /* --------------------------------- 
            SAMConnection 
           --------------------------------- */  
        class SAMConnection {  
        //  var $debug = true;  
          var $debug = false;  
          
          var $errno = 0;  
          var $error = '';  
          
          var $connection;  
          
          /* --------------------------------- 
              Create 
             --------------------------------- */  
          function Create($proto) {  
              if ($this->debug) e("SAMConnection.Create(proto=$proto)");  
              $rc = false;  
              /* search the PHP config for a factory to use...    */  
              $x = get_cfg_var('sam.factory.'.$proto);  
              if ($this->debug) t('SAMConnection.Create() get_cfg_var() "'.$x.'"');  
          
              /* If there is no configuration (php.ini) entry for this protocol, default it.  */  
              if (strlen($x) == 0) {  
                  /* for every protocol other than MQTT assume we will use XMS    */  
                  if ($proto != 'mqtt') {  
                      $x = 'xms';  
                  } else {  
                      $x = 'mqtt';  
                  }  
              }  
          
              /* Invoke the chosen factory to create a real connection object...   */  
              $x = 'sam_factory_'.$x.'.php';  
              if ($this->debug) t("SAMConnection.Create() calling factory - $x");  
              $rc = include $x;  
          
              if ($this->debug && $rc) t('SAMConnection.Create() rc = '.get_class($rc));  
              if ($this->debug) x('SAMConnection.Create()');  
              return $rc;  
          }  
          
          /* --------------------------------- 
              Constructor 
             --------------------------------- */  
          function SAMConnection() {  
            if ($this->debug) e('SAMConnection()');  
          
            if ($this->debug) x('SAMConnection()');  
          }  
          
          /* --------------------------------- 
              Commit 
             --------------------------------- */  
          function Commit() {  
            if ($this->debug) e('SAMConnection.Commit()');  
            $rc = true;  
          
            if (!$this->connection) {  
                $errno = 106;  
                $error = 'No active connection!';  
                $rc = false;  
            } else {  
                /* Call the method on the underlying connection object...   */  
                $rc = $this->connection->commit($target, $options);  
                $this->errno = $this->connection->errno;  
                $this->error = $this->connection->error;  
                if (!$rc) {  
                    if ($this->debug) t("SAMConnection.Commit() commit failed ($this->errno) $this->error");  
                    $rc = false;  
                }  
            }  
          
            if ($this->debug) x("SAMConnection.Commit() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              Connect 
             --------------------------------- */  
          function Connect($proto='', $options=array()) {  
            if ($this->debug) e('SAMConnection.Connect()');  
            $rc = false;  
          
            if ($proto == '') {  
                $errno = 101;  
                $error = 'Incorrect number of parameters on connect call!';  
                $rc = false;  
            } else {  
                $this->connection = $this->create($proto);  
                if (!$this->connection) {  
                    $errno = 102;  
                    $error = 'Unsupported protocol!';  
                    $rc = false;  
                } else {  
                    if ($this->debug) t("SAMConnection.Connect() connection created for protocol $proto");  
          
                    $this->connection->setdebug($this->debug);  
          
                    /* Call the connect method on the newly created connection object...   */  
                    $rc = $this->connection->connect($proto, $options);  
                    $this->errno = $this->connection->errno;  
                    $this->error = $this->connection->error;  
                    if (!$rc) {  
                       if ($this->debug) t("SAMConnection.Connect() connect failed ($this->errno) $this->error");  
                    } else {  
                       $rc = true;  
                    }  
                }  
            }  
          
            if ($this->debug) x("SAMConnection.Connect() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              Disconnect 
             --------------------------------- */  
          function Disconnect() {  
            if ($this->debug) e('SAMConnection.Disconnect()');  
            $rc = true;  
          
            if (!$this->connection) {  
                $errno = 106;  
                $error = 'No active connection!';  
                $rc = false;  
            } else {  
                /* Call the method on the underlying connection object...   */  
                $rc = $this->connection->Disconnect();  
                $this->errno = $this->connection->errno;  
                $this->error = $this->connection->error;  
                if (!$rc) {  
                    if ($this->debug) t("SAMConnection.Disconnect() Disconnect failed ($this->errno) $this->error");  
                } else {  
                    $rc = true;  
                    $this->connection = false;  
                }  
            }  
          
            if ($this->debug) x("SAMConnection.Disconnect() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              IsConnected 
             --------------------------------- */  
          function IsConnected() {  
            if ($this->debug) e('SAMConnection.IsConnected()');  
            $rc = true;  
          
            if (!$this->connection) {  
                $errno = 106;  
                $error = 'No active connection!';  
                $rc = false;  
            } else {  
                /* Call the method on the underlying connection object...   */  
                $rc = $this->connection->isconnected();  
                $this->errno = $this->connection->errno;  
                $this->error = $this->connection->error;  
                if (!$rc) {  
                    if ($this->debug) t("SAMConnection.IsConnected() isconnected failed ($this->errno) $this->error");  
                    $rc = false;  
                }  
            }  
          
            if ($this->debug) x("SAMConnection.IsConnected() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              Peek 
             --------------------------------- */  
          function Peek($target, $options=array()) {  
            if ($this->debug) e('SAMConnection.Peek()');  
            $rc = true;  
          
            if (!$this->connection) {  
                $errno = 106;  
                $error = 'No active connection!';  
                $rc = false;  
            } else {  
                /* Call the method on the underlying connection object...   */  
                $rc = $this->connection->peek($target, $options);  
                $this->errno = $this->connection->errno;  
                $this->error = $this->connection->error;  
                if (!$rc) {  
                    if ($this->debug) t("SAMConnection.Peek() peek failed ($this->errno) $this->error");  
                    $rc = false;  
                }  
            }  
          
            if ($this->debug) x("SAMConnection.Peek() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              PeekAll 
             --------------------------------- */  
          function PeekAll($target, $options=array()) {  
            if ($this->debug) e('SAMConnection.PeekAll()');  
            $rc = true;  
          
            if (!$this->connection) {  
                $errno = 106;  
                $error = 'No active connection!';  
                $rc = false;  
            } else {  
                /* Call the method on the underlying connection object...   */  
                $rc = $this->connection->peekall($target, $options);  
                $this->errno = $this->connection->errno;  
                $this->error = $this->connection->error;  
                if (!$rc) {  
                    if ($this->debug) t("SAMConnection.PeekAll() peekall failed ($this->errno) $this->error");  
                    $rc = false;  
                }  
            }  
          
            if ($this->debug) x("SAMConnection.PeekAll() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              Receive 
             --------------------------------- */  
          function Receive($target, $options=array()) {  
            if ($this->debug) e('SAMConnection.Receive()');  
            $rc = true;  
          
            if (!$this->connection) {  
                $errno = 106;  
                $error = 'No active connection!';  
                $rc = false;  
            } else {  
                /* Call the receive method on the underlying connection object...   */  
                $rc = $this->connection->receive($target, $options);  
                $this->errno = $this->connection->errno;  
                $this->error = $this->connection->error;  
                if (!$rc) {  
                    if ($this->debug) t("SAMConnection.Receive() receive failed ($this->errno) $this->error");  
                }  
            }  
          
            if ($this->debug) x("SAMConnection.Receive() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              Remove 
             --------------------------------- */  
          function Remove($target, $options=array()) {  
            if ($this->debug) e('SAMConnection.Remove()');  
            $rc = true;  
          
            if (!$this->connection) {  
                $errno = 106;  
                $error = 'No active connection!';  
                $rc = false;  
            } else {  
                /* Call the method on the underlying connection object...   */  
                $rc = $this->connection->remove($target, $options);  
                $this->errno = $this->connection->errno;  
                $this->error = $this->connection->error;  
                if (!$rc) {  
                    if ($this->debug) t("SAMConnection.Remove() remove failed ($this->errno) $this->error");  
                    $rc = false;  
                }  
            }  
          
            if ($this->debug) x("SAMConnection.Remove() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              Rollback 
             --------------------------------- */  
          function Rollback() {  
            if ($this->debug) e('SAMConnection.Rollback()');  
            $rc = true;  
          
            if (!$this->connection) {  
                $errno = 106;  
                $error = 'No active connection!';  
                $rc = false;  
            } else {  
                /* Call the method on the underlying connection object...   */  
                $rc = $this->connection->rollback($target, $options);  
                $this->errno = $this->connection->errno;  
                $this->error = $this->connection->error;  
                if (!$rc) {  
                    if ($this->debug) t("SAMConnection.Rollback() rollback failed ($this->errno) $this->error");  
                    $rc = false;  
                }  
            }  
          
            if ($this->debug) x("SAMConnection.Rollback() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              Send 
             --------------------------------- */  
          function Send($target, $msg, $options=array()) {  
            if ($this->debug) e('SAMConnection.Send()');  
            $rc = true;  
          
            if (!$this->connection) {  
                $errno = 106;  
                $error = 'No active connection!';  
                $rc = false;  
            } else {  
                /* Call the send method on the underlying connection object...   */  
                $rc = $this->connection->send($target, $msg, $options);  
                $this->errno = $this->connection->errno;  
                $this->error = $this->connection->error;  
                if (!$rc) {  
                    if ($this->debug) t("SAMConnection.Send() send failed ($this->errno) $this->error");  
                    $rc = false;  
                }  
            }  
          
            if ($this->debug) x("SAMConnection.Send() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              SetDebug 
             --------------------------------- */  
          function SetDebug($option=false) {  
            if ($this->debug) e("SAMConnection.SetDebug($option)");  
          
            $this->debug = $option;  
          
            if ($this->connection) {  
                $this->connection->setdebug($option);  
            }  
          
            if ($this->debug) x('SAMConnection.SetDebug()');  
            return;  
          }  
          
          /* --------------------------------- 
              Subscribe 
             --------------------------------- */  
          function Subscribe($topic, $options=array()) {  
            if ($this->debug) e("SAMConnection.Subscribe($topic)");  
            $rc = true;  
          
            if (!$this->connection) {  
                $errno = 106;  
                $error = 'No active connection!';  
                $rc = false;  
            } else {  
                /* Call the subscribe method on the underlying connection object...   */  
                $rc = $this->connection->subscribe($topic, $options);  
                $this->errno = $this->connection->errno;  
                $this->error = $this->connection->error;  
                if (!$rc) {  
                    if ($this->debug) t("SAMConnection.Subscribe() subscribe failed ($this->errno) $this->error");  
                    $rc = false;  
                }  
            }  
          
            if ($this->debug) x("SAMConnection.Subscribe() rc=$rc");  
            return $rc;  
          }  
          
          /* --------------------------------- 
              Unsubscribe 
             --------------------------------- */  
          function Unsubscribe($sub_id) {  
            if ($this->debug) e("SAMConnection.Unsubscribe($sub_id)");  
            $rc = true;  
          
            if (!$this->connection) {  
                $errno = 106;  
                $error = 'No active connection!';  
                $rc = false;  
            } else {  
                /* Call the subscribe method on the underlying connection object...   */  
                $rc = $this->connection->unsubscribe($sub_id);  
                $this->errno = $this->connection->errno;  
                $this->error = $this->connection->error;  
                if (!$rc) {  
                    if ($this->debug) t("SAMConnection.Unsubscribe() unsubscribe failed ($this->errno) $this->error");  
                    $rc = false;  
                }  
            }  
          
            if ($this->debug) x("SAMConnection.Unsubscribe() rc=$rc");  
            return $rc;  
          }  
        }  
          
        /* --------------------------------- 
            SAMMessage 
           --------------------------------- */  
        class SAMMessage {  
          
          /* --------------------------------- 
              Constructor 
             --------------------------------- */  
          function SAMMessage($body='') {  
          
            if ($body != '') {  
                $this->body = $body;  
            }  
          
          }  
        }  
          
        ?>  
    
    
     
    
    SAM/sam_factory_mqtt.php
    [php] view plaincopy
    
        <?php  
        /* 
        +----------------------------------------------------------------------+ 
        | Copyright IBM Corporation 2006, 2007.                                      | 
        | All Rights Reserved.                                                 | 
        +----------------------------------------------------------------------+ 
        |                                                                      | 
        | Licensed under the Apache License, Version 2.0 (the "License"); you  | 
        | may not use this file except in compliance with the License. You may | 
        | obtain a copy of the License at                                      | 
        | http://www.apache.org/licenses/LICENSE-2.0                           | 
        |                                                                      | 
        | Unless required by applicable law or agreed to in writing, software  | 
        | distributed under the License is distributed on an "AS IS" BASIS,    | 
        | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      | 
        | implied. See the License for the specific language governing         | 
        | permissions and limitations under the License.                       | 
        +----------------------------------------------------------------------+ 
        | Author: Dave Renshaw                                                 | 
        +----------------------------------------------------------------------+ 
         
        $Id: sam_factory_mqtt.php,v 1.1 2007/02/02 15:40:41 dsr Exp $ 
         
        */  
        require_once('MQTT/sam_mqtt.php');  
        return new SAMConnection_MQTT();  
        ?>  
    
    
     
    
    SAM/sam_factory_xms.php
    [php] view plaincopy
    
        <?php  
        /* 
        +----------------------------------------------------------------------+ 
        | Copyright IBM Corporation 2006, 2007.                                      | 
        | All Rights Reserved.                                                 | 
        +----------------------------------------------------------------------+ 
        |                                                                      | 
        | Licensed under the Apache License, Version 2.0 (the "License"); you  | 
        | may not use this file except in compliance with the License. You may | 
        | obtain a copy of the License at                                      | 
        | http://www.apache.org/licenses/LICENSE-2.0                           | 
        |                                                                      | 
        | Unless required by applicable law or agreed to in writing, software  | 
        | distributed under the License is distributed on an "AS IS" BASIS,    | 
        | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      | 
        | implied. See the License for the specific language governing         | 
        | permissions and limitations under the License.                       | 
        +----------------------------------------------------------------------+ 
        | Author: Dave Renshaw                                                 | 
        +----------------------------------------------------------------------+ 
         
        $Id: sam_factory_xms.php,v 1.1 2007/02/02 15:40:00 dsr Exp $ 
         
        */  
        if (!class_exists('SAMXMSConnection')) {  
            global $eol;  
            $l = (strstr(PHP_OS, 'WIN') ? 'php_' : '').'sam_xms.'.(strstr(PHP_OS, 'WIN') ? 'dll' : 'so');  
            echo $eol.'<font color="red"><b>Unable to access SAM XMS capabilities. Ensure the '.$l.' library is defined as an extension.</b></font>'.$eol;  
            return false;  
        } else {  
            return new SAMXMSConnection();  
        }  
        ?>  
    安卓和苹果推送源代码示例  
    
    2012-09-27 09:51:24|  分类: 苹果安卓开发 |字号 订阅
    //////////////////=================== 安卓苹果推送 =====================////////////////////
    
    //安卓推送方法封装
    /*
    | 服务器下载地址:http://www.cnblogs.com/hanyonglu/archive/2012/03/16/2399655.html
    | --------------------------------------------------------
    |    形式参数:
    |    (1)$message:要推送的消息内容
    |    (2)$broadcast:要推送是否为广播模式,取值为"Y"或"N"
    |    (3)$deviceID:要推送机器的硬件标识码
    |    返回结果:无
    |    调用示例:
    |    sys_push_android("TEST_BROADCAST","Y");//广播模式示例
    |    sys_push_android("TEST_SINGLE","N","af05df6969a3400caed26576be400745");//单个推送示例
    | --------------------------------------------------------
    */
    function sys_push_android($message,$broadcast,$deviceID = NULL)
    
    {
    //安卓推送服务器地址:
    //$post_server='http://192.168.1.146:7070/notification.do?action=send';
    $post_data = array();
    $broadcast = strtoupper($broadcast);//转换为大写
    $post_data['broadcast'] = $broadcast;
    //$post_data['title'] = $message;
    $post_data['title'] = '妈咪掌中宝';
    $post_data['message'] = $message;
    $post_data['uri'] = "";
    if($broadcast != "Y" && !empty($deviceID))
    $post_data['username'] = $deviceID;
    $url_str="";
    foreach ($post_data as $key=>$value)
    {
    $url_str .= "$key=".urlencode($value)."&";
    }
    $post_data=substr($url_str,0,-1);//去掉最后一个&号
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL,$post_server);
    ////为了支持cookie,暂时屏蔽
    //curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //此参数非常重要,可以防止短信服务器的返回内容,直接输出到客户端
    curl_exec($ch);
    curl_close($ch);
    }
    
    //苹果推送方法封装
    /*
    | --------------------------------------------------------
    |    形式参数:
    |    (1)$message:要推送的消息内容
    |    (2)$deviceID:要推送机器的硬件标识码
    |    返回结果:无
    | --------------------------------------------------------
    */
    function sys_push_iphone($message,$deviceID)
    {
    //选择开发环境:
    $passphrase = '123456';
    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
    stream_context_set_option($ctx, 'ssl', 'local_cert', $_SERVER['DOCUMENT_ROOT'].'/'.SYS_WEB_NAME.'/web/webservice/iphone_push_debug.pem');//正式环境需要使用iphone_push_run
    // Open a connection to the APNS server
    $fp = stream_socket_client(    'ssl://gateway.sandbox.push.apple.com:2195', $err,    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);    
    ////whb:如果连接成功则提醒未能连接苹果服务器,暂时屏蔽。 
    ////if (!$fp)    exit("Failed to connect: $err $errstr" . PHP_EOL);    
    ////echo 'Connected to APNS' . PHP_EOL;
    $body['aps'] = array('alert' => $message,'sound' => 'default');    
    $payload = json_encode($body);
    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceID) . pack('n', strlen($payload)) . $payload;    
    // Send it to the server
    $result = fwrite($fp, $msg, strlen($msg));    
    // Close the connection to the server
    fclose($fp);
    }
    
    //系统推送总封装函数
    /*
    | --------------------------------------------------------
    |    形式参数:
    |    (1)$device_type:设备类型 1:苹果 2:安卓
    |    (2)$deviceID:要推送机器的硬件标识码
    |    (3)$content:要推送的通知内容
    |    返回结果:无
    |    用法备忘:
    | --------------------------------------------------------
    */
    function sys_push_notice($device_type,$deviceID,$content)
    {
    if($device_type == 1)
    {
    sys_push_iphone($content,$deviceID);
    }
    if($device_type == 2)
    {
    sys_push_android($content,'N',$deviceID);//N表示是非广播模式     
    }    
    }




    实际操作
    $post_data = implode('&',$post_data);
                                $url='http://cqtv.sinosns.cn/app/index.php/tui';
                                $ch = curl_init();
                                //print_r($post_data);exit;
                                curl_setopt($ch, CURLOPT_POST, 1);
                                curl_setopt($ch, CURLOPT_URL,$url);
                                //传递一个作为HTTP “POST”操作的所有数据的字符串。
                                curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
                                ob_start();
                                curl_exec($ch);
                                $result = ob_get_contents() ;
                                ob_end_clean();
                                echo $result;
    
    
    
    
    
    $alert=$_POST['msg'];
    
    // 这里是我们上面得到的deviceToken,直接复制过来(记得去掉空格)
    $deviceToken=$_POST['token'];
    //$deviceToken = '99972361c17f1443c2b9c75deda6cef698d8e60011784f8dcf799275a07332f1';
    //print_r($alert);exit;
    // Put your private key's passphrase here:
    
    $passphrase = '111111';
    
    // Put your alert message here:
    
    //$message = 'My first push test!';
    $message=array('aps' => array('alert' => $alert, 'badge' => 1, 'sound'=>'default'));
    
    ////////////////////////////////////////////////////////////////////////////////
    
    print_r(dirname(__FILE__));exit;
    $pem = dirname(__FILE__).'/'.'sbone.pem';
    $ctx = stream_context_create();
    
    stream_context_set_option($ctx, 'ssl', 'local_cert', $pem);
    
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
    
    // Open a connection to the APNS server
    
    //这个为正是的发布地址
    
    //$fp = stream_socket_client(“ssl://gateway.push.apple.com:2195“, $err, $errstr, 60, //STREAM_CLIENT_CONNECT, $ctx);
    
    //这个是沙盒测试地址,发布到appstore后记得修改哦
    
    $fp = stream_socket_client(
    
        'ssl://gateway.sandbox.push.apple.com:2195', $err,
    
        $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
    
    
    if (!$fp)
    
        exit("无法连接到苹果推送服务器: $err $errstr" . PHP_EOL);
    
    echo "连接到苹果推送服务器</br>";
    
    // Encode the payload as JSON
    
    $payload = json_encode($message);
    //echo $payload;
    
    // Build the binary notification
    
    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
    
    // Send it to the server
    
    $result = fwrite($fp, $msg, strlen($msg));
    
    if (!$result)
        echo 'Message not delivered' . PHP_EOL;
    else
        echo '推送成功';
    fclose($fp);
    
    
    
     
  • 相关阅读:
    如何添加和删除本地存储中的数据?
    本地存储和cookies之间的区别是什么?
    那么如何使用WebSQL?
    WebSQL是HTML 5规范的一部分吗?
    WebSQL是什么?
    什么是本地存储的生命周期?
    本地存储和cookies之间的区别是什么?
    什么是多线程中的上下文切换?
    web workers是什么,为什么我们需要web workers?
    连接点?
  • 原文地址:https://www.cnblogs.com/afei-happy/p/3497692.html
Copyright © 2011-2022 走看看