2013-10-16 68 views
0

我正在用REST_controller庫在CodeIgniter中開發Web服務。它只是一個管理請求的控制器,我對每個方法都有一個請求。其中一種方法我試圖用它來發送蘋果通知,所以我需要使用ck.pem文件(它是一個證書)。我知道問題是我沒有.pem文件的好路徑。我應該在codeigniter中放置.pem文件以發送Apple推送通知?

這是我正在使用的代碼。我有控制器文件夾中的ck.pem文件,它不起作用。

function pushtest_get(){ 
     $data = array(); 
    // Put your device token here (without spaces): 
     $deviceToken = 'be67171fb22f3ea8fa68c13a9f2ba7229caf3a487aa656b20f769fa2b1fa5b7a'; 
    // Put your private key's passphrase here: 
     $passphrase = 'miguel'; 
     $message = 'Notification de prueba desde servicio web rest'; 
    //////////////////////////////////////////////////////////////////////////////// 
     $ctx = stream_context_create(); 
     stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem'); 
     stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 
    // 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); 

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

     $data['response'] = '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) 
      $data['response'] = 'Message not delivered' . PHP_EOL; 
     else 
      $data['response'] = 'Message successfully delivered' . PHP_EOL; 

    // Close the connection to the server 
     fclose($fp); 
     $response->skills = $data; 
     $this->response($response, 200); 
    } 

回答

0

你或許可以把它放在任何你想要的(只要Web服務器有權限讀取),只需使用完整的文件路徑:

stream_context_set_option($ctx, 'ssl', 'local_cert', '/var/certs/ck.pem'); 

stream_context_set_option($ctx, 'ssl', 'local_cert', '/var/www/codeigniter/application/controllers/ck.pem'); 

不過,我不會將它保存在控制器文件夾中。這會讓那些可能從事這個項目的人感到困惑,並且可能會讓你很難在未來找到答案。

我會在某個地方創建證書目錄並將其存儲在那裏。

0

可能是你應該保持一個文件夾中說certs在你的項目的根說:

/certs/ck.pem 

然後:

stream_context_set_option($ctx, 'ssl', 'local_cert', './certs/ck.pem'); 
+0

對不起,但這是行不通的。 – croigsalvador

0

不要緊,你有PEM文件。但好的做法是將其放在根目錄中。

檢查像發展港口沙箱的正確的環境是2195和生產是2196

還要檢查你的開發和生產過程中產生正確的PEM文件,並確保您傳遞正確passpharase值。

希望它可以幫助你:)

1

,因爲它一個多星期了,但我想分享,因爲我今天遇到了同樣的問題,您的問題可能得到解決。

對於CodeIgniter項目,您可以將ck.pem文件與application /目錄保持平行,然後下面的代碼將按原樣運行。

stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');