我需要用 php 服务器实现 apple 推送通知。我认为连接正常工作,但苹果服务器出于某种原因拒绝了该消息。
我收到这个错误:
"error 0errorString
\nFatal error: Call to undefined function socket_close() in /home/www/76cdd1fbdfc5aca0c9f07f489ecd9188/web/mobileApp/handleRequest.php on line 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";
谢谢
最佳答案
我猜您的 PHP 版本可能没有安装或启用套接字功能,这就是您收到该错误的原因。
取自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.
但是对于您的代码,您不需要使用那个socket_close($apns)
函数。相反,您可以删除它并使用 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.
所以使用 fclose($apns);
将关闭 Stream
关于php - 苹果推送通知..被拒绝?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7419931/