如何与 PHP 建立 SSL 连接
我需要开发一个 PHP 类来与 Apple 服务器通信,以便进行推送通知(APNS)。我有证书(.pem),并且尝试遵循在互联网上找到的各种教程,但尝试使用流套接字连接到 ssl://gateway.sandbox.push.apple.com:2195 时仍然遇到错误:
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'apns-dev.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);
A telnet此 URL 有效,因此端口 2195 打开。 自从我使用 phpinfo() 获得“注册流套接字传输:tcp、udp、ssl、sslv3、sslv2、tls”后,Openssl 在 PHP 上被激活。 我的证书已阅读完毕(PHP is_read(certif.pem) 在文件上返回 true)
是否有其他东西可以在 Apache 或 PHP 中激活以使其正常工作?
I need to develop a PHP class to communicate with Apple servers in order to do Push notification (APNS). I have the certificate (.pem) and I tried to follow various tutorials found on Internet but I'm still getting error trying to connect to ssl://gateway.sandbox.push.apple.com:2195 with stream socket :
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'apns-dev.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);
A telnet on this URL works so port 2195 is opened.
Openssl is activated on PHP since I get "Registered Stream Socket Transports : tcp, udp, ssl, sslv3, sslv2, tls" with a phpinfo().
My certificate is well read (PHP is_readable(certif.pem) returns true on the file)
Is there anything else to activate in Apache or PHP to get it work ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不完全是主题,但值得一提:
为了使 SSL 与 PHP 一起工作,有两件事很重要(如果您从源代码编译 PHP)
1)确保安装了 openssl 基础包和开发包
例如 yum install openssl openssl-devel
2) 使用 openssl 支持编译 PHP:
即在运行配置时使用 --with-openssl 选项。
然后,ssl就会出现在phpinfo()中的“Registered Stream Socket Transports”中
Not exactly on topic, but worth a mention:
Two things are important in order to make SSL work with PHP (if you are compiling PHP from source)
1) make sure the openssl base and devel packages are installed
e.g. yum install openssl openssl-devel
2) compile PHP with openssl support:
i.e. use the --with-openssl option when running config.
Then, ssl will appear in the "Registered Stream Socket Transports" in phpinfo()
我的第一条评论是你不会使用 apache。要与 APNS 通信,您必须将 PHP 脚本作为 shell 脚本(基本上是守护进程)运行。您需要打开与苹果服务的套接字连接。请参阅
socket_connect()
http://www .php.net/manual/en/function.socket-connect.php 开始使用。请记住,您需要保持连接打开,这意味着 PHP 脚本不应在单次执行后退出,而基本上保持接近无限循环。在每次迭代中检查更改以推送、写入套接字、睡眠、重复。
Apple 的开发者网站上有大量信息。从这里开始 http://developer.apple.com/iphone/library/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW1
简单地用 Google 搜索
APNS with PHP
产生了一些结果,其中之一是 http://code.google.com/p/php-apns/ 看起来很有希望。My first comment is you won't be using apache. To communicate with APNS you'll have to run your PHP script as a shell script (a daemon basically). You'll need to open a socket connection with apple's services. See
socket_connect()
http://www.php.net/manual/en/function.socket-connect.php to get started.Remember you need to keep the connection open which means the PHP script should NOT exit after a single execution but basically maintain a near infinite loop. On each iteration check for changes to push, write to the socket, sleep, repeat.
There's plenty of information on Apple's developer site. Start here http://developer.apple.com/iphone/library/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW1
A brief bit of googling
APNS with PHP
produced a few results one of which is http://code.google.com/p/php-apns/ which looks promising.找到了!问题出在证书上。由于我没有自己创建它,所以我没有调查它,但我错了......
我决定按照以下说明正确地重新生成它: 在 PHP 中使用 ssl 证书时出错,它可以工作!
感谢您的帮助 :)
Found it ! Problem was with the certificate. Since I did not create it myself I wasn't investigating it but I was wrong...
I decided to regenerate it myself properly following these instructions : Error using ssl cert with PHP and it works !
Thanks for your help :)