2013-01-07 78 views
1

當我在終端下面的腳本來運行它給人的錯誤:帶有PHP腳本的Apple推送通知不起作用?

Warning: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages: 
error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure in 
/Applications/MAMP/htdocs/SimplePush/simplepush.php on line 21 

Warning: stream_socket_client(): Failed to enable crypto in /Applications/MAMP/htdocs/SimplePush/ 
simplepush.php on line 21 

Warning: stream_socket_client(): unable to connect to ssl://gateway.push.apple.com:2195 
(Unknown error) in /Applications/MAMP/htdocs/SimplePush/simplepush.php on line 21 
Failed to connect: 0 

下面是代碼:

<?php 

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

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

// Put your alert message here: 
$message = 'Want more credits!'; 


$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.push.apple.com:2195', $err, 
$errstr, 60, 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

線#21的腳本是空的,請修復該代碼段。 –

+0

確保您正在使用您的生產證書,因爲您嘗試連接到APNS生產。 – Joe

+0

@Joe我正在使用開發證書。我不是爲了生活目的而準備的。 –

回答

1

因爲它聽起來像你所使用的開發證書,你可能想嘗試指向:gateway.sandbox.push.apple.com:2195。 URL gateway.push.apple.com:2195用於生產證書。

1

老問題,但我面臨同樣的問題。我解決它通過更改下面的代碼:

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

stream_context_set_option($ctx, 'ssl', 'local_cert', '/Users/Ohemod/Desktop/ck.pem'); 

劇本一直在尋找ck.pem文件,但不提供實際的目錄,我得到了錯誤信息,每次。如果有人面臨同樣的問題,請嘗試第二個。記住你的文件的目錄和我的代碼目錄不一樣。把你的ck.pem文件的實際目錄鏈接。

0

https://curl.haxx.se/ca/cacert.pem下載cacert.pem並將其保存在PHP腳本可用的相同文件夾中。

包含語句stream_context_set_option($ ctx,'ssl','cafile','cacert.pem');行$ ctx = stream_context_create();之後

FYI:

語法:布爾stream_context_set_option(資源$ stream_or_context,串$包裝物,繩$選項,混合$值)

對於其他選項檢查http://php.net/manual/en/context.ssl.php

0

我用你的腳本就像那樣,我找到的解決方案是從CURL調用,在php腳本中調用它:

這是適用於我的代碼。記住通過HTTPS

腳本1(調用通過捲曲一個)來稱呼它:

// set post fields 
    $post = [ 
     'mensaje' => 'THE MESSAGE TO SEND', 
     'device_id' => $device_id, 
    ]; 

    $ch = curl_init('https://www.DOMAIN/push-curl-script.php'); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    $response = curl_exec($ch); 
    curl_close($ch); 

推捲曲的script.php:

$certificado_push = "*****.pem"; 

    // Put your device token here (without spaces): 
    $deviceToken  = $_POST["device_id"]; 

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

    // Put your alert message here: 
    $message   = $_POST["mensaje"]; 


    $ctx = stream_context_create(); 
    stream_context_set_option($ctx, 'ssl', 'local_cert', $certificado_push); 
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase); 

    // Open a connection to the APNS server 
    $fp = stream_socket_client(
    'ssl://gateway.'.($sandbox ? 'sandbox.' : '').'push.apple.com:2195', $err, 
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx); 

    if (!$fp) { 
      echo "Failed to connect: $err $errstr" ; 
    }else{  
      echo 'Connected to APNS' ; 

      // 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' ; 
      }else{ 
       echo 'Message successfully delivered' ; 
      } 

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