Home > Backend Development > PHP Tutorial > php ios push (code)_PHP tutorial

php ios push (code)_PHP tutorial

WBOY
Release: 2016-07-21 15:01:47
Original
846 people have browsed it

Copy code The code is as follows:

//php needs to enable ssl (OpenSSL) support
$ apnsCert = "ck.pem";//Certificate permission file when connecting 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 and test server
$deviceToken = "a8fcd4aa8943b223d4ebcd54fe168a8b99b3f24c63dbc0612db25a8c0a588675";//ios device id, there can be no spaces in the middle, each ios Device 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 message
fclose ($apns);
if ($result)
echo "Sending message successfully: " . $payload;
else
echo 'Message not delivered';
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327979.htmlTechArticleCopy the code as follows: ?php //php needs to enable ssl (OpenSSL) support $apnsCert = "ck.pem ";//Certificate permission file when connecting to APNS, the certificate needs to be created as required $pass = "123456";/...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template