I need to develop a PHP class to communicate with Apple servers in order to do Push notification (APNS). I have the certificate (.pem) and I tried to follow various tutorials found on Internet but I'm still getting error trying to connect to ssl://gateway.sandbox.push.apple.com:2195 with stream socket :
$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);
A telnet on this URL works so port 2195 is opened.
Openssl is activated on PHP since I get "Registered Stream Socket Transports : tcp, udp, ssl, sslv3, sslv2, tls" with a phpinfo().
My certificate is well read (PHP is_readable(certif.pem) returns true on the file)
Is there anything else to activate in Apache or PHP to get it work ?
Not exactly on topic, but worth a mention:
Two things are important in order to make SSL work with PHP (if you are compiling PHP from source)
1) make sure the openssl base and devel packages are installed
e.g. yum install openssl openssl-devel
2) compile PHP with openssl support:
i.e. use the --with-openssl option when running config.
Then, ssl will appear in the "Registered Stream Socket Transports" in phpinfo()
My first comment is you won't be using apache. To communicate with APNS you'll have to run your PHP script as a shell script (a daemon basically). You'll need to open a socket connection with apple's services. See socket_connect()
http://www.php.net/manual/en/function.socket-connect.php to get started.
Remember you need to keep the connection open which means the PHP script should NOT exit after a single execution but basically maintain a near infinite loop. On each iteration check for changes to push, write to the socket, sleep, repeat.
There's plenty of information on Apple's developer site. Start here http://developer.apple.com/iphone/library/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW1
A brief bit of googling APNS with PHP
produced a few results one of which is http://code.google.com/p/php-apns/ which looks promising.
Found it ! Problem was with the certificate. Since I did not create it myself I wasn't investigating it but I was wrong...
I decided to regenerate it myself properly following these instructions : Error using ssl cert with PHP and it works !
Thanks for your help :)