ApnsPHP 无法连接到 Apple 网关

Posted

技术标签:

【中文标题】ApnsPHP 无法连接到 Apple 网关【英文标题】:ApnsPHP can't connect to Apple Gateway 【发布时间】:2011-06-08 13:08:40 【问题描述】:

今天突然出现一个非常奇怪的错误,说它无法连接到适当的网关...有什么修复建议吗?

这是记录器的输出:

Wed, 08 Jun 2011 15:05:44 +0200 Apnsphp[21724]: INFO: Trying ssl://gateway.sandbox.push.apple.com:2195...
Wed, 08 Jun 2011 15:05:45 +0200 ApnsPHP[21724]: ERROR: Unable to connect to 'ssl://gateway.sandbox.push.apple.com:2195':  (0)
Wed, 08 Jun 2011 15:05:45 +0200 ApnsPHP[21724]: INFO: Retry to connect (1/3)...
Wed, 08 Jun 2011 15:05:46 +0200 ApnsPHP[21724]: INFO: Trying ssl://gateway.sandbox.push.apple.com:2195...
Wed, 08 Jun 2011 15:05:47 +0200 ApnsPHP[21724]: ERROR: Unable to connect to 'ssl://gateway.sandbox.push.apple.com:2195':  (0)
Wed, 08 Jun 2011 15:05:47 +0200 ApnsPHP[21724]: INFO: Retry to connect (2/3)...
Wed, 08 Jun 2011 15:05:48 +0200 ApnsPHP[21724]: INFO: Trying ssl://gateway.sandbox.push.apple.com:2195...
Wed, 08 Jun 2011 15:05:48 +0200 ApnsPHP[21724]: ERROR: Unable to connect to 'ssl://gateway.sandbox.push.apple.com:2195':  (0)
Wed, 08 Jun 2011 15:05:48 +0200 ApnsPHP[21724]: INFO: Retry to connect (3/3)...
Wed, 08 Jun 2011 15:05:49 +0200 ApnsPHP[21724]: INFO: Trying ssl://gateway.sandbox.push.apple.com:2195...

Fatal error: Uncaught exception 'ApnsPHP_Exception' with message 'Unable to connect to 'ssl://gateway.sandbox.push.apple.com:2195':  (0)' in /home/xxxx/xxxxx/ApnsPHP/Abstract.php:354

【问题讨论】:

请添加更多信息... 您的 ApnsPHP 配置是什么?你之前有什么改变吗? 不,没有进行任何更改 将此添加到脚本的开头 ini_set("display_errors", 1); 并查看是否有任何 php 错误(不要忘记在生产中删除此行) 不,没有任何 php 错误...可能是苹果自己的服务器有问题吗?我尝试了最初随 ApnsPHP 提供的“sample_push.php”,它得到了同样的错误.... 不,我有一个发送推送通知的网页(通过沙盒和“实时”服务器),我在大约 30 分钟前测试了这两个页面,它正在工作。 【参考方案1】:

这个解决方案对我有用。原始答案在Push notifications server implementation -

我找到了一个解决方案,我不知道它是否是最好的,但它确实有效。在 Abstract.php 上(这个文件是 apns-php 源代码的一部分)我已经注​​释了第 343 行。现在它看起来像这样:

$streamContext = stream_context_create(array('ssl' => array(
//'verify_peer' => isset($this->_sRootCertificationAuthorityFile),
'cafile' => $this->_sRootCertificationAuthorityFile,
'local_cert' => $this->_sProviderCertificateFile
)));

我真的不知道这条线有什么意义,但知道推送通知工作正常。

【讨论】:

它也适用于我....但我没有得到诀窍。 @saurabh 你是怎么理解的?? 不,我不确定这条线如何或为什么会导致问题。我也不太喜欢 PHP。但是在我遇到并修复了这个问题之后,APNSPHP 已经发布了一个新版本。希望它在新版本中没有任何问题。 github.com/duccio/ApnsPHP/tags 这意味着您正在禁用使用 Entrust 根证书颁发机构验证对等方,仔细检查您的根 CA 很可能您可能正在使用相同的文件进行 SSL 客户端身份验证和根 CA 如果您不想禁用验证对等体,这里是 ApnsPHP 证书指南 code.google.com/p/apns-php/wiki/CertificateCreation 获取您的根证书 2048 并在 $push->setRootCertificationAuthority('rootcert.pem') 行中使用它【参考方案2】:

我的情况是我更改第 58 行和第 59 行文件 ApnsPHP/Push.php 这 protected $_aServiceURLs = array( 'ssl://gateway.push.apple.com:2195', // Production environment 'ssl://gateway.sandbox.push.apple.com:2195' // Sandbox environment ); /< @type array Service URLs environments. */ protected $_aServiceURLs = array( 'gateway.push.apple.com:2195', // Production environment 'gateway.sandbox.push.apple.com:2195' // Sandbox environment ); /< @type array Service URLs environments. */

【讨论】:

这对我有用 - 猜测它一定是 php 7 的东西? 我不确定,但我认为是关于 php 配置环境。我没有在 php 7 上测试它。用 php 5.6 测试过 而不是删除 ssl:// ,应该通过删除来启用 php.ini 中的 ssl ;在 extension=php_openssl.dll 和重启 apache 服务器之前。 我的前缀是 tls:// 删除它也为我修复了它【参考方案3】:

您可以检查您是否安装了正确的证书。查看此 URL:https://code.google.com/p/apns-php/wiki/CertificateCreation 以生成“entrust_root_certification_authority.pem”文件。

您可以通过在 sample_push.php 上注释以下行来跳过证书验证:

    //$push->setRootCertificationAuthority('entrust_root_certification_authority.pem');

希望对您有所帮助。问候。

【讨论】:

苹果 pem 文件每 13 个月过期一次,需要更换。接受的答案可能是解决问题的最快方法,但它绕过了安全检查,这应该是基于最佳实践的接受答案。【参考方案4】:

请检查苹果文档中指定的端口。 您必须在提供商端系统上打开端口。

【讨论】:

在专用服务器上运行,一切都打开【参考方案5】:

我遇到了同样的错误。我用谷歌搜索了很多,并按照本指南的每一步,确保我的 pem 文件生成正确: https://code.google.com/p/apns-php/wiki/CertificateCreation

然后我运行下面的命令来检查到 apns 的安全链接是否正确:

 openssl s_client -connect gateway.sandbox.push.apple.com:2195 -cert apns-dev-cert.pem -key apns-dev-key.pem -CApath /etc/ssl/certs/Entrust_Root_Certification_Authority.pem 

按回车,我得到了这个提示:

Enter pass phrase for apns-dev-key.pem:

然后我意识到我忘记了为证书设置密码(在我的情况下是 dev.pem)

// Instantiate a new ApnsPHP_Push object
$this->push = new ApnsPHP_Push(
    ApnsPHP_Abstract::ENVIRONMENT_SANDBOX,
    'dev.pem'
);
// Set the Provider Certificate passphrase
$this->push->setProviderCertificatePassphrase($passphrase);
// Set the Root Certificate Autority to verify the Apple remote peer
$this->push->setRootCertificationAuthority('entrust_root_certification_authority.pem');
// Connect to the Apple Push Notification Service
$this->push->connect();
// Instantiate a new Message with a single recipient
$this->message = new ApnsPHP_Message($deviceToken);

为 apns pem 设置正确的密码,问题就解决了。

【讨论】:

【参考方案6】:

我有同样的问题,我做错的是为 SSL 客户端身份验证和根 CA 提供相同的证书,下面的代码对我有用

$push = new ApnsPHP_Push(
        ApnsPHP_Abstract::ENVIRONMENT_SANDBOX,
        'ck.pem'
    );

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

https://code.google.com/p/apns-php/wiki/CertificateCreation

【讨论】:

【参考方案7】:

开发者需要以不同的方式导出 APNS 证书及其密钥。如果两者都同时导出,则会出现此错误。

【讨论】:

以上是关于ApnsPHP 无法连接到 Apple 网关的主要内容,如果未能解决你的问题,请参考以下文章

无法使用应用程序网关通过 SSH 连接到规模集实例

无法通过 pushsharp 中的 tcp 客户端连接到 feedback.sandbox.apple.com

无法连接到 ssl://gateway.push.apple.com:2195(连接超时)

Apple 说即使 wifi 已连接也无法连接到 wifi [关闭]

无法使用 JWT 令牌连接到 Apple App Store API

App Store Connect 操作错误:无法连接到 Apple 的网络服务