欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php ios推送(代码)

程序员文章站 2022-06-13 14:48:57
复制代码 代码如下:
复制代码 代码如下:

<?php
//php需要开启ssl(openssl)支持
$apnscert    = "ck.pem";//连接到apns时的证书许可文件,证书需格外按要求创建
$pass        = "123456";//证书口令
$serverurl   = "ssl://gateway.sandbox.push.apple.com:2195";//push服务器,这里是开发测试服务器
$devicetoken = "a8fcd4aa8943b223d4ebcd54fe168a8b99b3f24c63dbc0612db25a8c0a588675";//ios设备id,中间不能有空格,每个ios设备一个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);//连接服务器
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);//发送消息
fclose ( $apns );
if ($result)
    echo "sending message successfully: " . $payload;
else
    echo 'message not delivered';
?>