php 无法连接到 ssl://gateway.sandbox.push.apple.com:2195(连接被拒绝)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19865598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection refused)
提问by shadowarcher
I have a problem, I'd like to send out push notifications using php, however I keep getting this error:
我有一个问题,我想使用 php 发送推送通知,但是我不断收到此错误:
Warning: stream_socket_client() [function.stream-socket-client]: unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection refused) in /home/colupon/public_html/iPhone/push/index.php on line 21
Failed to connect: 111 Connection refused
My code is as follows:
我的代码如下:
$deviceToken = '0f************************************************************78';
$passphrase = '************';
$message = 'My first push notification!';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
$filename = 'ckdev.pem';
stream_context_set_option($ctx, 'ssl', 'local_cert', $filename);
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) .
$payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
echo 'Message not delivered' . PHP_EOL;
else
echo 'Message successfully delivered'.PHP_EOL;
// Close the connection to the server
fclose($fp);
?>
I believe the problem is with the server I'm using because I posted the same code and the same .pem file on a different server and it sent the notification without any problem. I attempted to open up ports on the firewall for my server because I read that might cause this problem, but the same error message still showed up. Is there anything else I can do? Any help would be greatly appreciated, thanks!
我相信问题出在我使用的服务器上,因为我在不同的服务器上发布了相同的代码和相同的 .pem 文件,并且它毫无问题地发送了通知。我试图在防火墙上为我的服务器打开端口,因为我读到这可能会导致此问题,但仍然出现相同的错误消息。还有什么我可以做的吗?任何帮助将不胜感激,谢谢!
回答by HMagdy
This issue is common problem in Apple Push notification, to resolve this error you have to go through as follows:
此问题是 Apple 推送通知中的常见问题,要解决此错误,您必须执行以下操作:
Test your pem files locally and remotely in server if the problem is server go to 2 else build pem correct file.
Set up the permission wrong on the folder that had the certificate file. This worked for me:
如果问题是服务器转到 2 否则构建 pem 正确文件,则在服务器中本地和远程测试您的 pem 文件。
在包含证书文件的文件夹上设置权限错误。这对我有用:
chmod 755 your_folder_that_has_certificate_files
chmod 755 your_folder_that_has_certificate_files
3.Check connectivity of apns port 2195 from your hosting server as follows:
3. 检查 apns 端口 2195 从您的托管服务器的连接,如下所示:
run
跑
telnet gateway.push.apple.com 2195
if this is the problem
如果这是问题所在
Trying 17.172.233.36...
telnet: connect to address 17.172.233.36: Connection refused
you can solve this issue by opening the port 2195 on the production server. You can verify by following command $telnet gateway.push.apple.com 2195
您可以通过在生产服务器上打开端口 2195 来解决此问题。您可以通过以下命令验证 $telnet gateway.push.apple.com 2195
-bash-3.2# telnet gateway.push.apple.com 2195
Trying 17.149.38.141...
Connected to gateway.push.apple.com (17.149.38.141).
Escape character is '^]'.
Connection closed by foreign host.
回答by mavericks
In case of MAC, (built-in server was working fine using terminal but not through browser, for me, so i installed MAMP.)
在MAC 的情况下,(对我来说,内置服务器使用终端运行良好,但不能通过浏览器运行,所以我安装了 MAMP。)
1.Go to---> /Library/WebServer/Documents/----copy both phpand ckdev.pemfile here.
1.转到---> /Library/WebServer/Documents/----复制php和ckdev。pem文件在这里。
2 go to terminal-->$open /private/etc-->go to--->apache2>originals>httpd.config file--> **"#LoadModule php5_module libexec/apache2/libphp5.so", remove "#"..(perhaps, you would have to change the permission also..!)
2 去终端--> $open /private/etc-->去-->apache2>originals>httpd.config 文件--> **"#LoadModule php5_module libexec/apache2/libphp5.so",去掉" #"..(也许,你也必须更改权限..!)
then goto browser and check--> localhost/yourPhpFile.php
然后转到浏览器并检查--> localhost/yourPhpFile.php
In Case of Windows system,
在Windows 系统的情况下,
1.Install WAMP,
1.安装WAMP,
2.goto php.inifile--->search for this ";extension=php_openssl.dll" line and remove semicolon ";".
2.转到php.ini文件--->搜索“ ;extension=php_openssl.dll”这一行,去掉分号“ ;”。
3.click WAMP icon from right-bottom goto PHP>PHP Extensions>select php_openssl..
3.单击右下角的WAMP图标转到PHP>PHP扩展>选择php_openssl..
That's it..hope this may help further seekers.
就是这样..希望这可以帮助进一步的寻求者。
回答by Denis Kutlubaev
In my case the problem was that I forgot to setup my passphrase correctly in php script.
就我而言,问题是我忘记在 php 脚本中正确设置我的密码。
<?php
// Put your device token here (without spaces):
$deviceToken = 'xxx';
// Put your private key's passphrase here:
$passphrase = 'xxx';
回答by Kashif
To me, it was the SELinux issue.
对我来说,这是 SELinux 问题。
So in your /etc/selinux/config file, set the SELINUX=enforcingto SELINUX=disabled. And restart. Thats it.
因此,在您的 /etc/selinux/config 文件中,将SELINUX=enforcing设置为SELINUX=disabled。并重新启动。就是这样。
回答by David Seek
I just had the SAME error and I found the solution for my problem.
我刚刚遇到了相同的错误,我找到了解决我的问题的方法。
?mypassword‘
instead of 'mypassword'
?mypassword‘
代替 'mypassword'
Seems like notepad changed the quotes. This literally took me 4 hours... Hopefully someone will read this and save some time...
似乎记事本更改了引号。这真的花了我 4 个小时......希望有人会读到这个并节省一些时间......