1

我面临的问题是 APNS 的服务器端实现。创建的示例应用程序得到了令牌创建的服务器 php 代码创建者证书和所有在本地服务器上都可以与 mamp 完美配合。[也在其他机器上测试过]

我面临的问题是当在服务器机器中设置代码时,它显示“创建 ssl 套接字时出错”连接超时。

出现错误

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 

有任何想法吗?

找到并尝试过的解决方案

  1. 开放2195端口

是否有必要创建一个新证书并将其全部托管在服务器中?

编辑

当前使用的推送代码

<?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);
?>
4

1 回答 1

0

这意味着您使用错误的证书连接到沙盒 APNS。您不能将其用于沙盒和生产。您还需要禁用RootCertificationAuthority验证或将其正确设置为正确的 CA ->从此处下载 Entrust 的 CA,然后执行

$push->setRootCertificationAuthority('entrust.pem');

在尝试连接到 APNS 之前:

$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();

编辑当您标记此内容时,apns-php我假设您使用 APNSPHP lib 进行交付,但我现在看到您没有这样做,所以我的答案并不是真正针对您的问题-无论如何我都会留下它,因为它可能对使用 APNS PHP 的人有所帮助(我的示例代码就是为了它)。我建议你看看:https ://code.google.com/p/apns-php/然后为自己节省一些时间和负担。

于 2014-07-16T06:39:15.057 回答