PHP iOS push (code) _php Tips

Source: Internet
Author: User
Copy Code code as follows:

<?php
PHP needs to turn on SSL (OpenSSL) support
$apnsCert = "CK.PEM";//Certificate license file when connected to APNs, the certificate needs to be created as required
$pass = "123456";//Certificate Password
$SERVERURL = "ssl://gateway.sandbox.push.apple.com:2195";//push server, this is the development test server
$deviceToken = "a8fcd4aa8943b223d4ebcd54fe168a8b99b3f24c63dbc0612db25a8c0a588675";//ios device ID, no spaces in the middle, one ID per iOS device
$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, stream_client_connect| Stream_client_persistent, $streamContext);//Connect server
if ($apns) {
echo "Connection ok <br/>";
} 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 Message
Fclose ($apns);
if ($result)
echo "Sending message successfully:". $payload;
Else
Echo ' Not delivered ';
?>

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.