APN Error in Server Script

2019-06-09 22:40发布

问题:

I am getting this error in my php script , while sending payload data.

Warning: stream_socket_client() [function.stream-socket-client]:
Unable to set private key file `/Applications/XAMPP/xamppfiles/htdocs/test/apn/apns-dev.pem'
in /Applications/XAMPP/xamppfiles/htdocs/test/apn/push.php on line 42

Warning: stream_socket_client() [function.stream-socket-client]:
failed to create an SSL handle
in /Applications/XAMPP/xamppfiles/htdocs/test/apn/push.php on line 42

Warning: stream_socket_client() [function.stream-socket-client]:
Failed to enable crypto
in /Applications/XAMPP/xamppfiles/htdocs/test/apn/push.php on line 42

Warning: stream_socket_client() [function.stream-socket-client]:
unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error)
in /Applications/XAMPP/xamppfiles/htdocs/test/apn/push.php on line 42

What is the reason ? Do I need to change any settings? I have also installed the .pem file in the server.

Thanks

回答1:

Can you post the PHP code (push.php) you're using to connect to APN?

Some shots in the dark:
- Are both the certificate and private key in that one .pem file?
- Did you remove the password from the private key file, or are you setting it properly in your PHP code?
- Does the user running your script have the proper unix permissions to access/read the cert/key file?
- Can you access Apple's server from your machine? You can test by running telnet.

telnet gateway.sandbox.push.apple.com 2195


回答2:

I had this problem and the key generation process was the issue, there are two different openssl commands for certificate and key file whereas I was using the same for both. Here's how I generate certificate and remove password from private key file (assuming you have exported .p12 files) :

openssl pkcs12 -clcerts -nokeys -out aps-dev-cert.pem -in aps-dev-cert.p12
openssl pkcs12 -nocerts -out aps-dev-key.pem -in aps-dev-key.p12
openssl rsa -in aps-dev-key.pem -out aps-dev-key.unencrypted.pem
cat aps-dev-cert.pem aps-dev-key.unencrypted.pem > aps-dev.pem

Note the difference in the first two openssl commands.



回答3:

I had this problem, too. For me it worked after removing the explicit setting of the 'cipher' in the ssl options:

$context_options = [ 
    'ssl' => [ 
        'local_cert' => ..., 
        'passphrase' => ..., 
        'ciphers' => 'DES-CBC3-SHA'
    ] 
]; 
stream_context_set_option($stream_context, $context_options);

so after removing the line: 'ciphers' => 'DES-CBC3-SHA' it worked.