2015-06-27 88 views
13

我在我的PHP Laravel應用程序中使用推送通知。我創建了一個pem文件並進行了測試。在我的開發機器上使用它時,它會正確推送到移動設備。當我現在將整個項目推送到我的生產服務器並啓動pushnotif調用時,出現以下錯誤:Failed to enable crypto推送通知加密錯誤

我是否需要在生產服務器上創建特殊的pem文件?我說的不是「生產證書」我仍然想使用沙盒測試

<?php 

namespace App\Helpers; 
use Carbon\Carbon; 
use Illuminate\Support\Facades\Config; 

class PushNotificationHelper { 
    public function pushToResponder($deviceToken) { 
     // Set device token of mobile device and the passphrase for certification 
     $pushToken = $deviceToken; 
     $passphrase = Config::get('MFConfig.PushNotificationTest.passPhrase'); 
     $APNS = Config::get('MFConfig.PushNotificationTest.APNS'); 

     // Open new context for streaming and set certificate as well as passphrase 
     $ctx = stream_context_create(); 
     stream_context_set_option($ctx, 'ssl', 'local_cert', '../App/Certificates/ck.pem'); 
     stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 
     stream_context_set_option($ctx, 'ssl', 'cafile', '../App/Certificates/entrust_2048_ca.cer'); 

     // Open connection to APNS 
     $fp = stream_socket_client($APNS, $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 

     // If no connection could be made then fail with error, otherwise connect 
     if (!$fp) { 
      exit("Failed to connect: $err, $errstr" . PHP_EOL); 
     } 

     echo 'Connected to APNS' . PHP_EOL; 

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

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

     // Build the binary notification 
     $msg = chr(0) . pack('n', 32) . pack('H*', $pushToken) . pack('n', strlen($payload)) . $payload; 

     // Send to server 
     $result = fwrite($fp, $msg, strlen($msg)); 

     // If no result is available, the message was not delivered. 
     if(!$result) { 
      echo 'Message not delivered.' . PHP_EOL; 
     } else { 
      echo 'Message successfully delivered.' . PHP_EOL; 
     } 

     // Close connection 
     fclose($fp); 

    } 
} 

我測試用的連接:

openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert ck.pem -debug -showcerts -CAfile entrust_2048_ca.cer 

,並返回狀態0(OK)這是罰款我認爲? 但是當我調用我得到的功能:failed loading cafile stream

+0

https://github.com/immobiliare/ApnsPHP/你有沒有檢查過這個倉庫。 – Ugur

回答

4

不確定這一點,但它可能是你在開發環境中的PHP設置。

嘗試找到cacert.pem文件。

如果您使用的是Linux系統,則可以使用終端嘗試locate cacert.pem

當你找到的位置找到php.ini文件,找到這一行:

;openssl.cafile =

,並更改爲 openssl.cafile=路徑從定位cacert.pem command或.PEM實際所處的位置。

報告它是如何去的。