PHP7,使用cURL支持HTTP2

3

当我通过PHP7+cURL向APNs发送推送消息时,我收到了错误消息。

错误消息:

�@@�HTTP/2 client preface string missing or corrupt. Hex dump for received bytes:

我猜是因为PHP7的原因,但不太确定。通过phpinfo()我可以看到没有加载mod_ssl,但从互联网上得知PHP7支持ssl自然而然地,因此openssl.so在系统中不存在。另外,在phpinfo()中看到的openssl版本是1.0.1e,这是与redhat7一起提供的版本,而不是我从源代码安装的版本。
我通过yum安装了php7,其余的openssl、nghttp和curl都是从源代码安装的。
Php代码:
$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTP_VERSION, 3);
//curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $alert);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("apns-topic: $apns_topic"));
curl_setopt($ch, CURLOPT_SSLCERT, $pemfile);
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, $pempwd);
$response = curl_exec($ch);

服务器使用的是 REDHAT 7 操作系统,环境信息如下。
$ openssl version
OpenSSL 1.0.2g  1 Mar 2016

curl --version
curl 7.48.0 (x86_64-pc-linux-gnu) libcurl/7.48.0 OpenSSL/1.0.2g nghttp2/1.9.2
Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp 
Features: IPv6 Largefile NTLM NTLM_WB SSL TLS-SRP HTTP2 UnixSockets 

$ php --version
PHP 7.0.5 (cli) (built: Apr  2 2016 13:08:13) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

$ curl --http2 -I https://nghttp2.org
HTTP/2.0 200
date:Sun, 17 Apr 2016 13:15:27 GMT
content-type:text/html
last-modified:Sat, 16 Apr 2016 14:56:04 GMT
etag:"57125284-1a0a"
accept-ranges:bytes
content-length:6666
x-backend-header-rtt:0.001459
strict-transport-security:max-age=31536000
server:nghttpx nghttp2/1.10.0-DEV
via:2 nghttpx
x-frame-options:SAMEORIGIN
x-xss-protection:1; mode=block
x-content-type-options:nosniff

即使我可以使用命令行向我的手机发送推送消息:

curl -d '{"aps":{"alert":"test message","sound":"default"}}' --cert /xxx/xxx.pem:xxxx -H "apns-topic:chs.itsme" --http2 https://api.development.push.apple.com/3/device/85f0257xxxxx

https://dev59.com/ClsW5IYBdhLWcg3wspAF#34831873 - Charlotte Dunois
谢谢。我看过那篇帖子,但没有解决我的问题。 - Allen Tsao
你是从源代码安装 PHP (以及/或在编译 OpenSSL 1.0.2g 源代码之前)?还是从仓库安装的? - Charlotte Dunois
我应该从源代码重新安装Php吗? - Allen Tsao
通过重新安装源代码中的PHP解决。 - Allen Tsao
显示剩余3条评论
1个回答

1
我遇到了同样的问题,发现主要原因是根CA证书
以下是我的环境:
  • curl : 7.48
  • openssl : 1.0.2g
  • php : 5.6.18

为什么会出现这个问题?

主要原因是证书。特别是根CA证书。当该证书不存在或在您的系统中路径错误时,可能会出现错误。

您需要了解如何使用CURLOPT_SSL_VERIFYPEER来解决此问题。 如果未定义CURLOPT_SSL_VERIFYPEER选项,则它将具有默认值true。此选项启用验证终端点主机ssl证书以避免安全问题,例如中间人攻击。并且在验证过程中使用已安装在您的系统上的根CA证书。

解决方案1

检查或安装根CA证书。 通常,它与openssl一起安装。如果遇到错误消息,则未安装在适当的路径或不存在。

因此,请使用以下命令检查文件。

$ php -r "var_dump(openssl_get_cert_locations());"

一个结果的示例:

array(8) {
  ["default_cert_file"]=>
  string(38) "/usr/local/openssl-1.0.2g/ssl/cert.pem"
  ["default_cert_file_env"]=>
  string(13) "SSL_CERT_FILE"
  ["default_cert_dir"]=>
  string(35) "/usr/local/openssl-1.0.2g/ssl/certs"
  ["default_cert_dir_env"]=>
  string(12) "SSL_CERT_DIR"
  ["default_private_dir"]=>
  string(37) "/usr/local/openssl-1.0.2g/ssl/private"
  ["default_default_cert_area"]=>
  string(29) "/usr/local/openssl-1.0.2g/ssl"
  ["ini_cafile"]=>
  string(0) ""
  ["ini_capath"]=>
  string(0) ""
}

在上面的代码中,缺省的证书文件路径是“/usr/local/openssl-1.0.2g/ssl/cert.pem”。你是否有根CA证书在那里?如果你有但位于不同的路径,请将其移动到默认的证书文件路径,并重命名为"cert.pem"。如果你没有它,需要下载它,例如http://curl.haxx.se/ca/cacert.pem$ wget http://curl.haxx.se/ca/cacert.pem 然后将它移动到默认的证书文件路径。
解决方案2
CURLOPT_SSL_VERIFYPEER => false 这个选项可以解决你的问题。但这可能会使你的系统暴露于“中间人攻击”之下。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接