IOS Push service-side PHP code

Source: Internet
Author: User

The blogger turned out to be a phper, and when he turned to iOS, he was particularly excited to see PHP.

There is currently a project that needs to build its own server-side code for iOS push. So search the code on the Internet, the band in the execution of the time will always error, errors are:

Warning:stream_socket_client (): SSL operation failed with code 1.

There was a problem with the secret key ck.pem that OpenSSL did not open or generate. Later found not these problems. Now put the correct code in case of a contingency:

<?phpset_time_limit (0); sleep (5);//  Here is the devicetoken we get above, copy it directly (remember to remove the space) $deviceToken  =  ' f2f66dbc7e3477df31cb32e1d56903efeec73d904b603bab9ba5a72948e1023b ';// put your private key ' S passphrase here: $passphrase  =  ' 123456 ';// put your alert message  here: $message  =  ' my first push test! '; $ctx  = stream_context _create (); Stream_context_set_option ($ctx,  ' SSL ',  ' allow_self_signed ',  true); stream_context_set_ Option ($ctx,  ' SSL ',  ' Verify_peer ',  false); Stream_context_set_option ($ctx,  ' SSL ',  ' Local_cert ',  ' Ck.pem ') stream_context_set_option ($ctx,  ' SSL ',  ' passphrase ',  $passphrase); / open a connection to the apns server//This is exactly the release address  //$fp  =  Stream_socket_client ("ssl://gateway.push.apple.com:2195", $err,  $errstr, 60, //stream_client_connect,  $ctx);//This is the sandbox test address, post to AppStore remember to modify OH   $fp  = stream_socket_client (' ssl://gateway.sandbox.push.apple.com:2195 ',  $err, $errstr,  60,  stream_client_connect| stream_client_persistent,  $ctx);//$FP =stream_socket_client ("udp://127.0.0.1:1113",  $err,  $ errstr, 60, stream_client_connect,  $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);//&NBSP;BUILD&NBSP;THE&NBSP;BINARY&NBSP;NOTIFICATION$MSG&NBSP;=&NBSP;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;elseecho  ' Message successfully delivered '  . php_eol;// close the connection to  the serverfclose ($FP);? >


IOS Push service-side PHP code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.