Skip to content

Instantly share code, notes, and snippets.

@samvermette
Created December 30, 2010 07:40
Show Gist options
  • Star 50 You must be signed in to star a gist
  • Fork 14 You must be signed in to fork a gist
  • Save samvermette/759564 to your computer and use it in GitHub Desktop.
Save samvermette/759564 to your computer and use it in GitHub Desktop.
Quickly send an Apple Push Notification using PHP
<?php
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsCert = 'apns-dev.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' => 'Oh hai!', 'badge' => 1, 'sound' => 'default');
$output = json_encode($payload);
$token = pack('H*', str_replace(' ', '', $token))
$apnsMessage = chr(0) . chr(0) . chr(32) . $token . chr(0) . chr(strlen($output)) . $output;
fwrite($apns, $apnsMessage);
socket_close($apns);
fclose($apns);
@bhavin-chauhan
Copy link

bhavin-chauhan commented Mar 12, 2019

got some errors
Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out) in /customers/d/b/3/server.com/httpd.www/app/apn-server.php on line 18 Warning: fwrite() expects parameter 1 to be resource, boolean given in /customers/d/b/3/server.com/httpd.www/app/apn-server.php on line 19 Warning: fclose() expects parameter 1 to be resource, boolean given in /customers/d/b/3/server.com/httpd.www/app/apn-server.php on line 20
Can anyone suggest what's wrong with this server.
If there is any restrictions from server then let me know what should I do.

@Harithareddykuram
Copy link

i am getting fwrite(): send of 92 bytes failed with errno=10053 An established connection was aborted by the software in your host machine. this error please help me.

@MOBitooT
Copy link

I am getting same errors as you @bhavin-chauhan. Did you manage to get this issue fixed?

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection refused) in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 17
Warning: pack(): Type H: illegal hex digit h in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 34

Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 35

Warning: fclose() expects parameter 1 to be resource, boolean given in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 42

Warning: Cannot modify header information - headers already sent by (output started at /home/mandin/public_html/vadmin2/send_broadcast_dev.php:42) in /home/mandin/public_html/vadmin2/common.php on line 977

Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 35

Warning: fclose() expects parameter 1 to be resource, boolean given in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 42

Can anyone help please???

@bhavin-chauhan
Copy link

I am getting same errors as you @bhavin-chauhan. Did you manage to get this issue fixed?

Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection refused) in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 17
Warning: pack(): Type H: illegal hex digit h in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 34

Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 35

Warning: fclose() expects parameter 1 to be resource, boolean given in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 42

Warning: Cannot modify header information - headers already sent by (output started at /home/mandin/public_html/vadmin2/send_broadcast_dev.php:42) in /home/mandin/public_html/vadmin2/common.php on line 977

Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 35

Warning: fclose() expects parameter 1 to be resource, boolean given in /home/mandin/public_html/vadmin2/send_broadcast_dev.php on line 42

Can anyone help please???

My Server not allow me to open APN Port number, So Finally I've Used Firebase service for that task. It's easy to use and both android, iOS token can easily work now. Let me know if you need any help from my side.
Thanks!
Bhavin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment