This article provides a detailed analysis of phpios push. For more information, see
The code is as follows:
// Ssl (OpenSSL) support must be enabled for php.
$ ApnsCert = "ck. pem"; // certificate license file used to connect to APNS. the certificate must be created as required.
$ Pass = "123456"; // certificate password
$ ServerUrl = "ssl: // gateway.sandbox.push.apple.com: 2195"; // push server, which is a development test server.
$ DeviceToken = "courier"; // ios device id. there cannot be spaces in the middle. Each ios device has one id.
$ Message = $ _ GET ['message'] or $ message = "hello! ";
$ Badge = (int) $ _ GET ['badge'] or $ badge = 2;
$ Sound = $ _ GET ['sound'] or $ sound = "default ";
$ Body = array ('aps '=> array ('alert' => $ message, 'badge' => $ badge, 'sound' => $ sound ));
$ StreamContext = stream_context_create ();
Stream_context_set_option ($ streamContext, 'SSL ', 'local _ cert', $ apnsCert );
Stream_context_set_option ($ streamContext, 'SSL ', 'passphrase', $ pass );
$ Apns = stream_socket_client ($ serverUrl, $ error, $ errorString, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ streamContext); // connect to the server
If ($ apns ){
Echo "Connection OK
";
} Else {
Echo "Failed to connect $ errorString ";
Return;
}
$ Payload = json_encode ($ body );
$ Msg = chr (0 ). pack ('N', 32 ). pack ('H * ', str_replace ('','', $ deviceToken )). pack ('N', strlen ($ payload )). $ payload;
$ Result = fwrite ($ apns, $ msg); // send a message
Fclose ($ apns );
If ($ result)
Echo "Sending message successfully:". $ payload;
Else
Echo 'message not delivered ';
?>