Php 通过Ajax使用流上下文发送apple推送通知
我正在尝试使用以下示例PHP脚本发送apple推送通知 : 此代码保存在一个页面sendPNFunction.php中 当我通过浏览器访问sendPNFunction.php时,一切正常。连接正常,消息已发送 但是,当我尝试使用jQuery通过Ajax调用访问页面时,每次连接都会失败Php 通过Ajax使用流上下文发送apple推送通知,php,ajax,apple-push-notifications,Php,Ajax,Apple Push Notifications,我正在尝试使用以下示例PHP脚本发送apple推送通知 : 此代码保存在一个页面sendPNFunction.php中 当我通过浏览器访问sendPNFunction.php时,一切正常。连接正常,消息已发送 但是,当我尝试使用jQuery通过Ajax调用访问页面时,每次连接都会失败 非常感谢您的帮助。php通常不关心如何调用它。“在地址栏中输入url并点击回车键”v.s.“在jquery中进行ajax调用”基本上都只是http调用,减去一些小的技术差异,就服务器和PHP而言,它们是完全相同的。
非常感谢您的帮助。php通常不关心如何调用它。“在地址栏中输入url并点击回车键”v.s.“在jquery中进行ajax调用”基本上都只是http调用,减去一些小的技术差异,就服务器和PHP而言,它们是完全相同的。我能够通过将证书文件移动到调用上述函数的页面所在的本地文件夹来解决这个问题。最初,我将它们存储在根目录中,但当通过Ajax调用时,这种配置是不可行的。
function sendPNotification(){
// Put your device token here (without spaces):
$deviceToken = '0f744707bebcf7......';
// Put your private key's passphrase here:
$passphrase = 'pu...';
// Put your alert message here:
$message = 'My first push notification!';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
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
$fp = stream_socket_client(
'ssl://gateway.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
//Proceed to send message if all is well......
}
sendPNotification();