Php APSN错误:推送服务器
我面临的问题是APN的服务器端实现。 已创建示例应用程序获取令牌 创建服务器php代码创建证书adn在本地服务器上与mamp完美结合。[在其他机器上也进行了测试] 我面临的问题是,当服务器计算机中设置的代码显示“创建ssl套接字时出错”连接超时 出现错误Php APSN错误:推送服务器,php,apple-push-notifications,apns-php,Php,Apple Push Notifications,Apns Php,我面临的问题是APN的服务器端实现。 已创建示例应用程序获取令牌 创建服务器php代码创建证书adn在本地服务器上与mamp完美结合。[在其他机器上也进行了测试] 我面临的问题是,当服务器计算机中设置的代码显示“创建ssl套接字时出错”连接超时 出现错误 Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com
Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out) in /home/push.php on line 16
有什么想法吗
找到并尝试的解决方案
<?php
function sendApnsPushMessage($arrtokens,$req_message,$req_title)
{
echo "Started APNS";
if(count($arrtokens)>0)
{
$apnsCert = 'ck.pem';
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
// Connect to Apple Push Notification server
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
stream_context_set_option($streamContext, 'ssl', 'passphrase', '1234');
//60 -> timeout in seconds
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 200, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $streamContext);
if (!$apns)
{
echo($error);
die('Error creating ssl socket');
}
stream_set_blocking ($apns, 0);
echo 'Connected to APNS' . PHP_EOL;
for($i=0;$i<count($arrtokens);$i++)
{
$tokenid = $arrtokens[$i];
echo($tokenid);
$body['aps'] = array(
'title' => $req_title,
'alert' => $req_message,
'sound' => 'default'
);
$payload = json_encode($body);
$correctingToken=str_replace(' ', '', $tokenid);
echo $correctingToken;
$msg = chr(0) . pack('n', 32) . pack('H*', $correctingToken) . pack('n', strlen($payload)) . $payload;
$result = fwrite($apns, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered' . PHP_EOL;
}
usleep(500000);
fclose($apns);
}
}
$token= "dd3e343519524a986f946db0f888e895961b370720ee1c9b2495d0a58542607e";
$req_title='test';
$req_message='text';
$arrayOfTokens = array($token);
sendApnsPushMessage($arrayOfTokens,$req_message,$req_title);
?>
这意味着您使用了错误的证书连接到沙盒APN。您不能将其用于沙箱和生产。此外,您还需要禁用
RootCertificationAuthority
verification或将其正确设置为right CA->,然后执行此操作
$push->setRootCertificationAuthority('entrust.pem');
在尝试连接到APN之前:
$push = new ApnsPHP_Push(ApnsPHP_Abstract::ENVIRONMENT_SANDBOX, 'sandbox.pem');
$push->setRootCertificationAuthority('entrust.pem');
$msg = new ApnsPHP_Message($device_token);
$msg->setText('Hello');
...
$push->add($msg);
$push->connect();
$push->send();
$push->disconnect();
EDIT当您将其标记为
apns php
时,我假设您使用APNSHP lib来进行交付,我现在看到您没有这样做,因此我的答案并非针对您的问题-我将保留它,因为它可能会帮助使用apns php的用户(我的示例代码就是针对它)。我建议你看一看:然后节省一些时间和负担。Marcin Orlowski:谢谢你的快速回答。我在问题中添加了代码。你能告诉我这个LOC需要设置在哪里吗?对不起,我是服务器端编程的完全初学者。我正在尝试在服务器中设置开发环境。我没有使用APNSHPP。我只是在实现直接代码。请参阅我记录的问题并编辑了我的答案。那你为什么不使用apns php呢?好的,我试试看。还有一件事,要发送到一组设备令牌,有可能吗?