2014-07-25 37 views
1

APNS服務在幾天前工作得很完美。唯一改變的是我將我的服務器從Ubuntu 12.10升級到了13.04。在升級之後,我的apns停止工作。我不知道爲什麼,但那是我在服務器上改變的唯一。APNS在PHP上不再工作了

我也嘗試telnet gateway.push.apple.com:2195與我從我的mac生成的證書和一切工作正常。

但是當我嘗試使用我的服務器用PHP代碼我得到這樣的響應:

連接失敗:0

  <?php 

    // Put your device token here (without spaces): 
    $deviceToken = 'bd218fb8ce00xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; 

     // Put your private key's passphrase here: 
    $passphrase = 'xxxxxxxxxxxxxxxxxxxx'; 

    // Put your alert message here: 
     $message = 'My first push notification!'; 

    //////////////////////////////////////////////////////////////////////////////// 

     $ctx = stream_context_create(); 
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'apns.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, 360, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 

    if (!$fp) 
     exit("Failed to connect: $err $errstr" . PHP_EOL); 

    echo 'Connected to APNS' . PHP_EOL; 

     // Create the payload body 
    $body['aps'] = array(
     'alert' => $message, 
     'sound' => 'default' 
     ); 

    // Encode the payload as JSON 
$payload = json_encode($body); 

// 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 'Message successfully delivered' . PHP_EOL; 

// Close the connection to the server 
fclose($fp); 
+0

確保您的pem文件可以從您的路徑到達 – Geet

回答

0

在格式化和安裝Ubuntu 14.10的全新副本之後,一切進展順利。 APNS再次像預期的那樣工作。

一切都表明升級搞砸了PHP或任何配置文件的東西。

所以上課不要從終端升級Ubuntu。

備份您的所有數據,並從光盤或USB安裝新版本的Ubuntu,如果您想升級它。用所有可能的更新重新安裝LAMP並恢復所有文件和數據。

之後一切正常。

0

嘗試把正確的證書路徑在PHP腳本即

stream_context_set_option($ctx, 'ssl', 'local_cert',APPPATH.'../assets/apns.pem');