I am working hard on it and openssl is enabled on my server but issue remains as it. Will it make any difference if run it MAc book. Please response if it is done there.
My Code:
<?php
$deviceToken = 'my device key'; // not putting in for security
$payload['aps'] = array('alert' => 'This is the alert text', 'badge' => 1, 'sound' => 'default');
$payload = json_encode($payload);
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'apns-dev.pem';
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);
socket_close($apns);
fclose($apns);
?>
Error:
Warning: stream_socket_client() [function.stream-socket-client]: SSL
operation failed with code 1. OpenSSL Error messages:
error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake
failure in F:\xampp\htdocs\apns\apns.php on line 24
Warning: stream_socket_client() [function.stream-socket-client]:
Failed to enable crypto in F:\xampp\htdocs\apns\apns.php on line 24
Warning: stream_socket_client() [function.stream-socket-client]:
unable to connect to ssl://gateway.sandbox.push.apple.com:2195
(Unknown error) in F:\xampp\htdocs\apns\apns.php on line 24
Please post if any have an idea.
Thanks
I had the same problem. The solution for me was generate the certificate again. I did it with these commands:
openssl x509 -in testpush_aps_development.cer -inform der -out TestPushCert.pem
openssl pkcs12 -nocerts -out TestPushKey.pem -in TestPush.p12
After that:
cat TestPushKey.pem TestPushCert.pem > TestPushCK.pem
Remember that testpush_aps_development.cer in this case is the certificate that you have in your apple developer page in the section APNS Certificates.
Hope this help you, anyway I'm sure it's a problem with the certificates.
Check your certificates, they cause errors quite oftenly.
Related
I'm working on push notification in IOS with php server and I produced the certificate and keys of the app also I'm ensure from unblocking port for ssl://gateway.sandbox.push.apple.com:2196 and 2195 but at all time I get this error during try to connect on ssl also I'm sure from the permission of all key files
Warning: stream_socket_client(): SSL: crypto enabling timeout in /Users/samahahmaed/Desktop/CER/newspush.php on line 24
Warning: stream_socket_client(): Failed to enable crypto in /Users/samahahmaed/Desktop/CER/newspush.php on line 24
Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /Users/samahahmaed/Desktop/CER/newspush.php on line 24
Failed to connect: 0
Edited:
When I'm trying this command
openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert PushCertificate.pem -key PushKey.pem -CApath /etc/ssl/certs/Entrust_Root_Certification_Authority.pem
I get this error
CONNECTED(00000003)
write:errno=54
php file:
<?php
// Put your device token here (without spaces):
$deviceToken = 'mydevicetokenhere';
// Put your private key's passphrase here:
$passphrase = '1234';
$message = $argv[1];
$url = $argv[2];
if (!$message || !$url)
exit('Example Usage: $php newspush.php \'Breaking News!\' \'https://raywenderlich.com\'' . "\n");
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apple_push_notification_production.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);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'link_url' => $url,
);
// 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);
I searched a lot about this issue and I tried all possible solutions but without any result what I can do now ?
Edited:
After add -debug to openssl command the most strange thing these lines:
stream_context_set_option($ctx, 'ssl', 'local_cert', 'apple_push_notification_production.pem');
This line looks like you are using production certificate to connect to sandbox APNS. Try using development certificate. You can get the same from apple developer dashboard.
I got the following error message, when trying to send a push notif via PHP:
stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure
I guess it could be the issue with the SSL3 problems in the past? But does this mean the script is not usable that way anymore? What do I need to change, as i have no clue. I checked all certificates and they are working. I can connect to the sandbox at apple via terminal with the certificates and the handshake seems to work via terminal.
This is my PHP Script
class PushNotification {
public function sendTestMessageToDevice($message){
$devicetoken = Config::get('mfsconfig.PushNotificationTest.deviceToken');
$passphrase = Config::get('mfsconfig.PushNotificationTest.passPhrase');
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open connection to APNS
$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);
}
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 a binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $devicetoken) . pack('n', strlen($payload)) . $payload;
// Send to server
$result = fwrite($fp, $msg, strlen($msg));
if(!result) {
echo 'Message not delivered' . PHP_EOL;
} else {
echo 'Message successfully delivered' . PHP_EOL;
}
fclose($fp);
}
I think I got the solution:
Click the disclosure arrow next to your certificate in Keychain Access and select the certificate and the key.
Right click and choose Export 2 items
Choose the p12 format from the drop down and name it cert.p12.
Now covert the p12 file to a pem file:
$ openssl pkcs12 -in cert.p12 -out apple_push_notification_production.pem -nodes -clcerts
After uploading the newly created pem file, everything works smoothly!
I have a rare situations for sending push notifications via php which I cant figure out. I have a simple php script that send the notification shown below. If I execute this file via the command line php script.php it works just fine. If I execute via web http://domain.com/script.php it give me a Permission Denied Error. I have the correct cert path, not password for the cert. Any ideas?
Warning: stream_socket_client(): unable to connect to ssl://gateway.push.apple.com:2195 (Permission denied)
...............
$apns = connect_apns('gateway.push.apple.com', '2195');
$write = send_payload($apns, $deviceToken, $payload);
fclose($apns);
}
function connect_apns($apnsHost, $apnsPort) {
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', 'cert.pem');
return stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 60, STREAM_CLIENT_CONNECT, $streamContext);
}
function send_payload($handle, $deviceToken, $payload) {
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
return fwrite($handle, $apnsMessage);
}
so after much time spending looking at this I noticed that I had Selinux enabled. If you want to keep it enabled you need to set the property httpd_can_network_connect to true by issuing this command: setsebool -P httpd_can_network_connect 1. Otherwise just just disable selinux by going to vim /etc/selinux/config and set SELINUX=disabled.
Hello I try to send Push Notifications via PHP over the FTP-Hoster square7.ch, but I always get a few errors. The certificate is on the server.
Can anyone help me?
Here's the PHP code:
<?php
$streamContext= stream_context_create();
stream_context_set_option($streamContext , 'ssl','local_cert' , 'TestPushApp.pem');
//stream_context_set_option($streamContext , 'ssl' , 'passphrase','password');
$socketClient = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195',$error,$errorString,60,STREAM_CLIENT_CONNECT,$streamContext);
$payload['aps']= array('alert' => 'Erste Push Nachricht ueber PHP','sound' => 'default','badge' => '20');
$payload= json_encode($payload);
echo $payload;
$deviceToken = str_replace(' ','','XXXXXXXXXXXXXXXXXX');
$message= pack('CnH*',0,32,$devicetoken);
$message= $message . pack ('n',strlen($payload));
$message= $messgae . $payload;
fwrite($socketClient,$message);
fclose($socketClient);
?>
Here's the error message:
Warning: stream_socket_client() [function.stream-socket-client]: SSL operation failed with code 1. OpenSSL Error messages: error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure in /users/michaellll/www/push.php on line 5
Warning: stream_socket_client() [function.stream-socket-client]: Failed to enable crypto in /users/michaellll/www/push.php on line 5
Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /users/michaellll/www/push.php on line 5
{"aps":{"alert":"Erste Push Nachricht ueber PHP","sound":"default","badge":"20"}}
Warning: fwrite() expects parameter 1 to be resource, boolean given in /users/michaellll/www/push.php on line 13
Warning: fclose() expects parameter 1 to be resource, boolean given in /users/michaellll/www/push.php on line 14
// Push Notification code for IPHONE in PHP
$deviceToken = $users_rows['gcm_regid'];
$passphrase = 'pass1234';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'DrinksterDevelopment.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 120, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
$body['aps'] = array(
// 'alert' => $_GET["message"].'#'.$_GET["type"].'#'.$_GET["deal_id"],
'alert' => $_GET["message"],
'sound' => 'default'
);
$body['other'] = $_GET["type"].'#'.$_GET["deal_id"];
$payload = json_encode($body);
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
$result_iphone = fwrite($fp, $msg, strlen($msg));
if (!$result_iphone)
$msg_iphone = 'Message not delivered' . PHP_EOL;
else
$msg_iphone = 'Message successfully delivered' . PHP_EOL;
mail('jackbrown00001#gmail.com', 'IOSPushMsgStatus', $msg_iphone);
fclose($fp);
}
I think its SSL issue, enable SSL then it might work. One more thing is there stream_context_set_option($streamContext , 'ssl','local_cert', 'TestPushApp.pem');
it must be like this
$options = array('ssl' => array('local_cert' => 'include/TestPushApp.pem','passphrase' => '//Provided passpharase to you'));
stream_context_set_option($streamContext , $options);
and if you are having production certificate i. e pem file then its good to use ssl://gateway.push.apple.com:2195
Here file writes function will contain two params only fwrite($fp, $msg); not the length of that packaged message.
I checked this url
and followed the steps to generated pem file from p12 file. Below is the code to generate the pem file.
....
if ($this->file->save($uploadDirectory . $filename . '.' . $ext)) {
$filenamewithpath = $uploadDirectory . $filename . '.' . $ext;
$handle = fopen($filenamewithpath, 'r');
$p12buf = fread($handle, filesize($filenamewithpath));
fclose($file);
$password = #$p12pwd;
$results = array();
$worked = openssl_pkcs12_read($p12buf, $results, $password);
//d($results); exit;
if ($worked) {
//echo '<pre>', print_r($results, true), '</pre>';
$new_password = null;
$result = null;
$worked = openssl_pkey_export($results['pkey'], $result, $new_password);
if($worked) {
//echo "<pre>It worked! Your new pkey is:\n", $result, '</pre>';
file_put_contents( $uploadDirectory . $filename . '.pem',$result);
return array(
'success' => true,
'filename'=>$filename . '.pem',
'uploaddir' =>$uploadDirectory,
);
} else {
return array('error' => openssl_error_string());
}
} else {
return array('error' => openssl_error_string());
}
}
....
I got it working fine and generated pem file is stored in given directory. Now i am trying to use this pem file for push notification. Check the below code,
<?php
$apnsHost = 'gateway.sandbox.push.apple.com';
//$apnsHost = 'gateway.push.apple.com';
$apnsCert = 'test.pem';
$apnsPort = 2195;
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
$payload['aps'] = array('alert' => 'this is test!', 'badge' => 1, 'sound' => 'default');
$output = json_encode($payload);
$token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$token = pack('H*', str_replace(' ', '', $token));
$apnsMessage = chr(0) . chr(0) . chr(32) . $token . chr(0) . chr(strlen($output)) . $output;
//var_dump($apnsMessage); exit;
fwrite($apns, $apnsMessage);
#socket_close($apns);
fclose($apns);
?>
When i ran this code, i got below error,
Warning: stream_socket_client(): Unable to set local cert chain file `test.pem'; Check that your cafile/capath settings include details of your certificate and its issuer in /var/www/html/myserver/apns/test.php on line 13
Warning: stream_socket_client(): failed to create an SSL handle in /var/www/html/myserver/apns/test.php on line 13
Warning: stream_socket_client(): Failed to enable crypto in /var/www/html/ela/apns/test.php on line 13
Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /var/www/html/myserver/apns/test.php on line 13
Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/html/myserver/apns/test.php on line 20
Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/html/myserver/apns/test.php on line 23
Kindly advice to fix this issue.
I would have called
'openssl pkcs12 -in cert.p12 -inpass pass:password ...something.. ..something...'
on the command line and tried to pick up or pipe its output back. Perhaps there is a wrapper for that in php, otherwise a system command seems the most easy route.
I had the same kind of issue when developing for iOS. We howerve got *.cer certificates and changed them to *.pem using the following command line:
openssl x509 -inform der -in aps_production_identity.cer -out aps_production_identity.pem
If this does not work make sure to include the private key as indicated here:
Apple Push Notification Service