Php 推送IOS 这段代码有问题吗?
大家看下 这个代码 对不对?
function tuisong(){<br /> //手机注册应用返回唯一的deviceToken <br /> $deviceToken = '7146a15ef9e1ebb8122c110e6fc970a454db3a9946ce24402481a564ee900e'; <br /> $deviceToken = $deviceToken; <br /> //ck.pem通关密码 <br /> $pass = 'yeyu'; //$pass = '123456'; <br /> //消息内容 <br /> $message = 'zao shang hao!'; <br /> //badge我也不知是什么 <br /> $badge = 4; <br /> //sound我也不知是什么(或许是推送消息到手机时的提示音) <br /> $sound = 'Duck.wav'; <br /> //建设的通知有效载荷(即通知包含的一些信息) <br /> $body = array(); <br /> //$body['id'] = "4f94d38e7d9704f15c000055"; <br /> $body['aps'] = array('alert' => $message,'issystem'=>1); <br /> if ($badge) <br /> $body['aps']['badge'] = $badge; <br /> if ($sound) <br /> $body['aps']['sound'] = $sound; <br /> //把数组数据转换为json数据 <br /> $payload = json_encode($body); <br /> print_r($payload);<br /> //echo strlen($payload),"\r\n"; <br /> <br /> //下边的写法就是死写法了,一般不需要修改, <br /> //唯一要修改的就是:ssl://gateway.sandbox.push.apple.com:2195这个是沙盒测试地址,ssl://gateway.push.apple.com:2195正式发布地址 <br /> <br /> $apnscert=$_SERVER['DOCUMENT_ROOT'].'data/ck.pem';<br /> print_r($apnscert);<br /> $ctx = stream_context_create(); <br /> var_dump($ctx);<br /> //stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); <br /> stream_context_set_option($ctx, 'ssl', 'local_cert', $apnscert); <br /> stream_context_set_option($ctx, 'ssl', 'passphrase', $pass); <br /> $fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx); <br /> //var_dump($fp);<br /> if (!$fp) { <br /> print "Failed to connect $err $errstr\n"; <br /> return; <br /> } <br /> else { <br /> //print "Connection OK\n<br/>"; <br /> } <br /> // send message (群发要循环)<br /> $msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload; <br /> print "Sending message :" . $payload . "\n"; <br /> fwrite($fp, $msg); <br /> fclose($fp); <br /> }
<br><?php <br /><br>class Push<br>{<br> public $deviceToken;<br> public $localcert = 'ck.pem';<br> public $passphrase = '11111';<br><br><br> private function createPayload($message, $type, $sound, $number, $mid)<br> {<br> $body = array("aps" => array("alert" => $message, "badge" => 1, "sound" => 'received5.caf'));<br> // Encode the payload as JSON<br> $payload = json_encode($body);<br><br> return $payload;<br> }<br><br> // Put your private key's passphrase here:<br> public function pushData($message, $type, $sound, $number, $mid)<br> {<br> $pem = dirname(dirname(__FILE__)) . '/' . $this->localcert;<br> if (!file_exists($pem)) {<br> echo '沒有找到密匙文件!' . PHP_EOL;<br> exit;<br> }<br> //echo $pem . PHP_EOL;//debug<br><br> $ctx = stream_context_create();<br> stream_context_set_option($ctx, 'ssl', 'local_cert', $pem);<br> stream_context_set_option($ctx, 'ssl', 'passphrase', $this->passphrase);<br><br> // Open a connection to the APNS server<br> //这个为正是的发布地址<br> //$fp = stream_socket_client(“ssl://gateway.push.apple.com:2195“, $err, $errstr, 60, //STREAM_CLIENT_CONNECT, $ctx);<br> <br> $serverURL = "ssl://gateway.push.apple.com:2195";<br> $sanBoxURL = "ssl://gateway.sandbox.push.apple.com:2195";<br> $fp = stream_socket_client($serverURL, $err, $errstr, 60, STREAM_CLIENT_CONNECT <div class="clear"> </div>