views:

342

answers:

2

Hi,

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 ?

A: 

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.

Matt S
Hum I don't understand why I would need to let the connection opened ? What if I juste want to open the connection, send one notification and then close connection ? Or more specific to my case, execute my PHP script once in a week to send news to my iphone's app.I already did those kind of searches and I am using APNS PHP
Anth0
On the link I provided to apple's website directly below the -Note- box there's a line: "You should also retain connections with APNs across multiple notifications. APNs may consider connections that are rapidly and repeatedly established and torn down as a denial-of-service attack. Upon error, APNs closes the connection on which the error occurred." In your case I suppose recreating the connection once a week ins't horribad but just be aware.
Matt S
Ok I keep this point in mind.That's because I'm already using APNS PHP code and because I think I'm doing everything fine on PHP side that I'm starting to think it's an Apache issue maybe...
Anth0
How are you 'executing' your script? By making a request in your web browser? Are you entering information into a text box that you then want pushed out? As I asked above what is the erorr you're receiving?
Matt S
I am executing the script in a we browser and message to be sent is hard coded for the moment. So calling my script in a browser execute the script and so the connection which is getting me the folowing errors :[function.stream-socket-client]: Unable to set local cert chain file `editus_dev_push.pem'; [function.stream-socket-client]: failed to create an SSL handle[function.stream-socket-client]: Failed to enable crypto [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error)
Anth0
Where does your `editus_dev_push.pem` cert site in relation to where your PHP script is? You also may want to add additional options from http://us2.php.net/manual/en/context.ssl.php such as `verify_peer` and `allow_self_signed`
Matt S
Since I am using Copix CMS and a few redirections are done I have put certificate under two directories : root directory (www) and script directory. I did that when I read this topic http://stackoverflow.com/questions/809682/error-using-ssl-cert-with-php but still doesn't work for me. Moreover, like I said, is_readable(mycertif.pem) returns true so I think certificate is found =/ I also tried those two options your are suggesting (they are by default in APNS PHP) but no success...
Anth0
http://stackoverflow.com/questions/2462280/iphone-sdk-push-notification-randomly-fails This may also relate to you considering what you're attempting to do.
Matt S
"To communicate with APNS you'll have to run your PHP script as a shell script" - not necessarily, APNS is specifically designed to work when the client is not permanently connected.
symcbean
APNS is designed for the client to not be constantly connected (client = iphone/ipod) but it is explicit in that the provider (the server and in this case the PHP script) to use persistent connections. In this case he's asking about the provider and not the client so I'm unsure what your comment is referring to.
Matt S
A: 

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 : http://stackoverflow.com/questions/809682/error-using-ssl-cert-with-php and it works !

Thanks for your help :)

Anth0