how to retrieve ck.pem file for push notifications php - php

Hi I'm having trouble sending push notifications from a cron job invoked php script. However the same script works when I invoke the script from the terminal so I know its a problem retrieving the ck.pem. When I hit the script from the terminal I just use the lines
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
to get the ck.pem and it works because the ck.pem file is right on my desktop and local. When I hit the script thats invoked by the cron job it does not have the ck.pem file locally so I uploaded the ck.pem file to the server so the php script invoked from the cron job could access it and this is the code I use to retrieve it
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', file_get_contents("http://www.website.com/Certificates/ck.pem"));
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
however I get the errors
PHP Warning: stream_socket_client(): Failed to enable crypto
PHP Warning: stream_socket_client(): unable to connect to
ssl://gateway.sandbox.push.apple.com:2195 (Unknown error)
PHP Warning: stream_socket_client(): SSL operation failed with code
1. OpenSSL Error messages:
and all these errors happen on the third line of code in the following segment:
$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;
So like I said Im pretty sure its because of the ck.pem file and I made sure that ports 2195 and 2196 are open so it is not that and I can't figure this out. Any help or suggestions would be greatly greatly appreciated!

Solved this answer with this stack overflow link: iOS push notification does not work when using crontab scheduler
I was retrieving the ck.pem file the wrong way

Related

Unable to connect to ssl://gateway.push.apple.com:2195 (Unknown error)

I have a question about Apples Push Service.
What do following warnings exactly mean?
Warning: stream_socket_client(): Failed to enable crypto in /opt/../xyz.php on line 150
Warning: stream_socket_client(): unable to connect to ssl://gateway.push.apple.com:2195 (Unknown error) in /opt/../xyz.php on line 150
I am using about 5 different php files for sending push (AddFriendPush, ChatMessagePush...) to the iOS Devices and they are working fine. But sometime I get these warnings and I don't know what they mean but push doesn't work when I get these warnings..
I know that they can appear if your pem file is wrong or the port 2195 is blocked. But push works sometimes and sometimes it doesn't. So the pem file has to be right and the port can't be blocked.
I know it is not good to have 5 different scripts for push, I will change it soon.
Do you have any idea what is going on?
My php scripts look like this:
function sendPush($deviceToken, $msg)
{
$deviceToken = $deviceToken;
$passphrase = 'passhrase';
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'Production.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client(
'ssl://gateway.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp) {
unset($con);
exit();
}
//do the other stuff
//Close the connection to the server
fclose($fp);
}

Apple Push Notification is not working on my godaddy shared server, it's throght failed to connect 111 connection refused php push notification error

I got a error like failed to connect 111 connection refused php push notification using APNS.
It's working fine on local server but not working on GoDaddy shared server.Please find my Php code.
$path_pem_file="http://dummy.com/uploads/app/1.pem";
// this is the pass phrase you defined when creating the key
$passphrase = '';
// this is where you can customize your notification
$payload = '{"aps":{"alert":"message","sound":"default"}}';
// start to create connection
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', $path_pem_file);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', 'Device token here..') . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if ($fp) { fclose($fp);}
You need to have following PORTs open on your server to use Apple APNs
2195
2196
As far as I know GoDaddy will not open this ports for you on shared hosting and you will have to go for Dedicated server or VPS.
If you are using GoDaddy Web Hosting, they will not open the ports to send push. If you are using GoDaddy Virtual Private Servers, then you can call them and get the ports unlocked.
You need to go for Dedicated Hosting.
Please have a look on the following url, this may help you :
http://www.buzztouch.com/forum/thread.php?tid=b580c417cbbc86eb9575ea0&currentpage=1

Correct way to reopen TCP connection with PHP after remote party closes?

I'm having issues reopening a TCP socket connection after its remotely closed. Specifically this is for Apple Push Notification service - when Apple encounters a device token from you that pairs with a device that deleted your application they close the secure TCP socket to their push gateway. I need to immediately reopen this connection (or start a new one) when this issue occurs so I can continue sending notification to other users who still do have the app installed. I have some code using which I try to sort this out but it's freezing my entire Apache server and I have to restart manually to get things running again and I have no idea why. Below is the code with which I attempt to reopen a socket with Apple:
if($error_response == "InvalidToken"){
$results .= "\ntried to restart";
fclose($fp);
usleep(20000);
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck_real.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', '****');
$fp = stream_socket_client('ssl://gateway.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
}

Generating push certificate ios php

I am trying to make a push certificate for using with my app. In development part.
I have the code for push notification in server already done but it has a part in which it says:
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'iOS_pushCertificate\ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
I need to create thatn ck.pem and also the passphrase. I don't know where do I have to set the passphrase or how to create the certificate.
Ray Wenderlich has a great tutorial in his page explaining exactly that.

Apple Push with proxy and stream_context

I have to send push notification to iOS devices. My connection has to be enabled through a proxy. I tried everything but without success. I have an error 110 Connection Timed Out. It's working with cURL if I just try to connect to Apple push's address. I don't know where the problem is. Proxy config ? PHP stream_context wrong implementation ?
Here's my code :
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'certificate.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', 'my_passphrase');
stream_context_set_option($ctx, 'ssl', 'verify_peer', false);
stream_context_set_option($ctx, 'http', 'proxy', 'tcp://my-proxy.net:8080');
stream_context_set_option($ctx, 'http', 'request_fulluri', true);
$fp = stream_socket_client('ssl://gateway.sandbox.push.apple.com:2195', $err,$errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
var_dump($fp);
var_dump($err);
var_dump($errstr);
exit;
Do you have an idea ?
EDIT:
Can it be directly linked to Squid ? I just figured out the proxy is running with Squid.
I also try with fopen() function instead of stream_socket_client() but it seems it doesn't allow ssl protocol.
Here's my var_dump outputs : bool(false) int(110) string(20) "Connection timed out"
I also have this warning : Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out) in /share/www/website/test.php on line 22
It could simply be your proxy does not allow port 2195 to be opened.
iPhone Push Notification Unable to Connect to the SSL Server
I guess you either:
Need to talk to the people who run the proxy to see if port 2195 is open or not.
or
Setup a test server listening on port 2195 and then try to do a test connection to it through the proxy. That should allow you to test if it is the proxy that is blocking the connection requests.
or
Test whether Curl can open the connection using a proxy.
Which is done by setting the options:
// sets the proxy to go through
curl_setopt($ch, CURLOPT_PROXY, $proxy);
// sets to use a tunnel proxy which most http proxies are
curl_setopt($ch, CURLOPT_HTTPTUNNELPROXY, $proxy);
Full testing code here.
create the SSL context
open a tcp socket to the proxy
send request to the proxy to connect to APNs
once connexion is accepted enable SSL
Have a look at my reply in this post: Send Push Notification to APNS through proxy

Categories