苹果推送通知..拒绝?

发布于 2024-12-04 14:05:32 字数 1246 浏览 1 评论 0原文

我需要使用 php 服务器实现苹果推送通知。我认为连接工作正常,但由于某种原因,该消息被苹果服务器拒绝。

我收到此错误:

“错误 0errorString
\n致命错误:调用未定义 函数socket_close()在 /home/www/76cdd1fbdfc5aca0c9f07f489ecd9188/web/mobileApp/handleRequest.php 在线 420
\n"

这是我的简单代码(带有虚假消息,仅供测试):

$payload['aps'] = array('alert' => 'This is the alert text', 'badge' => unread, 'sound' => 'default');
    $payload['server'] = array('serverId' => $serverId, 'name' => $name);
    $payload = json_encode($payload);

    $apnsHost = 'gateway.sandbox.push.apple.com';
    $apnsPort = 2195;
    $apnsCert = 'ck.pem';

    $streamContext = stream_context_create();
    stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

    $apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);

    echo "error " . $error;
    echo "errorString" . $errorString;

    $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
    fwrite($apns, $apnsMessage);

    socket_close($apns);
    fclose($apns);


    echo "Sent";

谢谢

I need to implement apple push notifications with a php server. I think the connection is correctly working but the message is refused by apple server for some reason.

I'm getting this error:

"error 0errorString
\nFatal error: Call to undefined
function socket_close() in
/home/www/76cdd1fbdfc5aca0c9f07f489ecd9188/web/mobileApp/handleRequest.php
on line 420
\n"

This is my simple code (with a fake message, just for testing):

$payload['aps'] = array('alert' => 'This is the alert text', 'badge' => unread, 'sound' => 'default');
    $payload['server'] = array('serverId' => $serverId, 'name' => $name);
    $payload = json_encode($payload);

    $apnsHost = 'gateway.sandbox.push.apple.com';
    $apnsPort = 2195;
    $apnsCert = 'ck.pem';

    $streamContext = stream_context_create();
    stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);

    $apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);

    echo "error " . $error;
    echo "errorString" . $errorString;

    $apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
    fwrite($apns, $apnsMessage);

    socket_close($apns);
    fclose($apns);


    echo "Sent";

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

悲凉≈ 2024-12-11 14:05:32

我猜您的 PHP 版本可能没有安装或启用套接字功能,这就是您收到该错误的原因。

摘自 PHP.net

此处描述的套接字函数是 PHP 扩展的一部分,必须在编译时通过提供 --enable-sockets 选项进行配置来启用它。

但对于您的代码,您不需要使用该 socket_close($apns) 函数。相反,您可以删除它并使用关闭,因为 (引用自 PHP.net ):

成功时会返回一个流资源,该资源可以与其他文件函数(例如 fgets()、fgetss()、fwrite()、fclose() 和 feof())一起使用,失败时为 FALSE。< /p>

因此使用 fclose($apns); 将关闭 Stream

I'm guessing your PHP version may not have the Sockets functionality installed or enabled which is why you are getting that error.

Taken from PHP.net:

The socket functions described here are part of an extension to PHP which must be enabled at compile time by giving the --enable-sockets option to configure.

But for your Code, you don't need to use that socket_close($apns) function. Instead you can just remove it and use the close since (Quote from PHP.net):

On success a stream resource is returned which may be used together with the other file functions (such as fgets(), fgetss(), fwrite(), fclose(), and feof()), FALSE on failure.

So using fclose($apns); will close the Stream

计㈡愣 2024-12-11 14:05:32

没有关闭套接字的功能,只能创建套接字并通过 fwrite/fclose 等方式管理它:
http://php.net/manual/en/function.stream-socket -client.php

There is no function to close socket, you can only create socket and manage it by fwrite/fclose etc:
http://php.net/manual/en/function.stream-socket-client.php

牛↙奶布丁 2024-12-11 14:05:32

替换:

socket_close($apns);

with

fclose($apns);

这应该可以解决您报告的错误:但不确定这是否会真正解决您的 APNS 问题。

Replace:

socket_close($apns);

with

fclose($apns);

That should solve the error you've reported: not sure if that will actually fix your issue with APNS, though.

故人的歌 2024-12-11 14:05:32

我其实已经解决了。问题在于消息的内容(空)。套接字功能完美运行。

I've actually solved. The problem was the content of the message (empty). The socket function works perfectly.

初见你 2024-12-11 14:05:32
$apnsHost = 'gateway.sandbox.push.apple.com';

从此网址中删除“沙箱”,它将运行。

$apnsHost = 'gateway.sandbox.push.apple.com';

Remove "sandbox" from this url and it will run.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文