Php interface for IOSAPNS push

Source: Internet
Author: User
Php interface for IOSAPNS push
 0, 'MSG '=> 'Error request']);} if (! Isset ($ _ GET ['device _ token']) {exit (json_encode (['code' => 0, 'MSG '=> 'device information missing']);} if (! Isset ($ _ GET ['message']) {exit (json_encode (['code' => 0, 'MSG '=> 'push information missing']);} if (strlen ($ _ GET ['message'])> 256) {exit (json_encode (['code' => 0, 'MSG '=> 'push message length exceeds 256 characters']);} if (! Isset ($ _ GET ['app _ id']) {exit (json_encode (['code' => 0, 'MSG '=> 'appid missing']);} $ apps = ['000000' => ['cert' => '. /cert/1. pem ', // The pem generated by ios, 'password' => '1', // The pem password 'debug' => '1 ', // Development Mode 1 production mode 0]; if (! Array_key_exists ($ _ GET ['app _ id'], $ apps) {exit (json_encode (['code' => 0, 'MSG '=> 'appid error']);} $ ctx = stream_context_create (); stream_context_set_option ($ ctx, 'SSL', 'local _ cert ', $ apps [$ _ GET ['app _ id'] ['cert']); stream_context_set_option ($ ctx, 'SSL ', 'passphrase ', $ apps [$ _ GET ['app _ id'] ['password']); // Open a connection to the APNS serverif ($ apps [$ _ GET ['app _ id'] ['debug']) {$ fp = stream_socket_c Lient ('SSL: // container: 100', $ err, $ err_str, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ ctx);} else {$ fp = stream_socket_client ('SSL: // gateway.push.apple.com: 2195 ', $ err, $ err_str, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ ctx);} if (! $ Fp) {exit (json_encode (['code' => 0, 'MSG '=>' connection to the APN server failed, error code :'. $ err. ', error message :'. $ err_str]);} $ body ['aps '] = array ('alert' => $ _ GET ['message'], 'sound' => 'default'); $ payload = json_encode ($ body); // Build the binary notification $ msg = chr (0 ). pack ('N', 32 ). pack ('H * ', $ _ GET ['device _ token']). pack ('N', strlen ($ payload )). $ payload; // Send it to the server $ result = fwrite ($ fp, $ msg, strlen ($ msg )); Fclose ($ fp); if (! $ Result) {exit (json_encode (['code' => 0, 'MSG '=>' message :'. $ result]);} else {exit (json_encode (['code' => 1, 'MSG '=>' message :'. $ result]);}

Pem generation policy see: http://my.oschina.net/u/2340880/blog/413584.

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.