/** Apple Message Push method
* $deviceToken Apple device token
* $message message content*/ functionIosmsg_send ($deviceToken,$message){ $message=strlen($message) >40?mb_substr ($message, 0,40, ' Utf-8 '):$message; $ctx=stream_context_create(); stream_context_set_option($ctx, ' SSL ', ' Local_cert ',substr(dirname(__file__), 0,-30). ' Test\productionpush.pem ');//Certificate Path stream_context_set_option($ctx, ' SSL ', ' Passphrase ', ' 123456 '); //Open A connection to the APNS server//This is the official release address $fp=stream_socket_client(' ssl://gateway.push.apple.com:2195 ',$err,$errstr, Stream_client_connect,$ctx); //This is the sandbox test address, after posting to AppStore remember to modify//$fp = stream_socket_client (' ssl://gateway.sandbox.push.apple.com:2195 ', $err, $err STR, stream_client_connect| Stream_client_persistent, $ctx); if(!$fp) Exit("Failed to connect:$err $errstr" .Php_eol); Echo' Connected to APNS '.Php_eol; //Create the payload body $body[' APS '] =Array( ' Alert ' =$message, ' sound ' = ' default ' ); //Encode the payload as JSON $payload= Json_encode ($body); //Build the binary notification $msg=CHR(0).Pack(' n ', 32).Pack(' h* ',$deviceToken).Pack(' n ',strlen($payload)).$payload; //Send It to the server $result=fwrite($fp,$msg,strlen($msg)); if(!$result) Echo' Message not delivered '.Php_eol; Else Echo' Message successfully delivered '.Php_eol; //Close The connection to the server fclose($fp); }
PHP Apple message push