iOS APNS推送Java、c# P12證書和Php pem證書製做

Java、c# P12證書:http://www.jianshu.com/p/4282...
Php pem證書製做:http://blog.csdn.net/dztianyu...php

有可能出現的錯誤:json

1.執行php push.php時報錯 :c#

Warning: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in /Users/songhua/Desktop/推送證書文件/Php-Push/push.php on line 30

Warning: stream_socket_client(): Failed to enable crypto in /Users/songhua/Desktop/推送證書文件/Php-Push/push.php on line 30

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /Users/songhua/Desktop/推送證書文件/Php-Push/push.php on line 30
Failed to connect 0

解決方案:數組

去下載https://entrust2048.bj.bcebos... 網關文件並在push.php文件里加入stream_context_set_option($ctx,'ssl','cafile','entrust_2048_ca.cer'); app

php推送代碼,entrust_2048_ca.cer、pem和php必須在同一個目錄下:socket

<?php  
//手機註冊應用返回惟一的deviceToken  
// $deviceToken = 'c112ed059d3bc019833f9eb991cd9e594583381dd979b7a6f809f683efd70d82';  
$deviceToken = '0191498cf8704bf9c09a624855f0ad0e0b65933d33e9b296223745b22d8779c8';  
//ck.pem通關密碼  
$pass = '123456';     
//消息內容  
$message = 'A test message!';  
//badge我也不知是什麼  
$badge = 4;  
//sound我也不知是什麼(或許是推送消息到手機時的提示音)  
$sound = 'Duck.wav';  
//建設的通知有效載荷(即通知包含的一些信息)  
$body = array();  
$body['id'] = "4f94d38e7d9704f15c000055";  
$body['aps'] = array('alert' => $message);  
if ($badge)  
  $body['aps']['badge'] = $badge;  
if ($sound)  
  $body['aps']['sound'] = $sound;  
//把數組數據轉換爲json數據  
$payload = json_encode($body);  
echo strlen($payload),"\r\n";  
  
//下邊的寫法就是死寫法了,通常不須要修改,  
//惟一要修改的就是:ssl://gateway.sandbox.push.apple.com:2195這個是沙盒測試地址,ssl://gateway.push.apple.com:2195正式發佈地址  
$ctx = stream_context_create();  
// $pem = dirname(__FILE__) .'/'.'phpck.pem';
stream_context_set_option($ctx, 'ssl', 'local_cert', 'phpck.pem');
stream_context_set_option($ctx,'ssl','cafile','entrust_2048_ca.cer');   
stream_context_set_option($ctx, 'ssl', 'passphrase', $pass);  
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);  
if (!$fp) {  
    print "Failed to connect $err $errstr\n";  
    return;  
}  
else {  
   print "Connection OK\n<br/>";  
}  
// send message  
$msg = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;  
print "Sending message :" . $payload . "\n";    
fwrite($fp, $msg);  
fclose($fp);  
?>
相關文章
相關標籤/搜索