首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当curl或file_get_contents请求https url时,php崩溃。

当curl或file_get_contents请求https url时,php崩溃。
EN

Stack Overflow用户
提问于 2013-07-03 03:33:02
回答 2查看 2.4K关注 0票数 3

我的服务器是nginx + php-fpm。

下面的代码将导致错误

代码语言:javascript
复制
file_get_contents('https://github.com');

代码语言:javascript
复制
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://github.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch); //crash here
curl_close($ch);

网页显示502错误。

nginx日志

错误2656#0:*541 recv()失败(104:从上游读取响应标头时连接重置)

fpm日志

07月03日:37:37.619903通知fpm_got_signal(),第48行:接收 07月03日:37:37.619926警告fpm_children_bury(),第215行:子3567 (池默认)退出信号11 SIGSEGV (核心转储)后417.576755秒开始 07月03日:37:37.620807通知fpm_children_make(),第352行:子4193 (池默认)启动

如果请求url以http://开头,则一切正常。

php配置命令是

代码语言:javascript
复制
'./configure' '--prefix=/www/nginx_php-5.2.17' '--with-config-file-path=/www/nginx_php-5.2.17/etc' '--with-mysql=/www/mysql' '--with-iconv=/usr' '--with-freetype-dir' '--with-jpeg-dir' '--with-png-dir' '--with-zlib' '--with-libxml-dir=/usr' '--enable-xml' '--disable-rpath' '--enable-discard-path' '--enable-inline-optimization' '--with-curl' '--enable-mbregex' '--enable-mbstring' '--with-mcrypt=/usr' '--with-gd' '--enable-gd-native-ttf' '--with-openssl' '--with-mhash' '--enable-ftp' '--enable-sockets' '--enable-zip' '--enable-fastcgi' '--enable-fpm' '--with-fpm-conf=/www/etc/php-fpm.conf'
EN

回答 2

Stack Overflow用户

发布于 2013-07-03 04:10:43

尝试添加以下两个:

代码语言:javascript
复制
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

他们将阻止对SSL证书的验证。可能是问题所在,因为验证可能失败。除非必须验证源代码,否则在使用cURL下载数据时始终要添加这2行代码。

PS:不过不确定这会对你有帮助。

票数 1
EN

Stack Overflow用户

发布于 2021-03-16 19:38:21

我最近不得不处理一个类似的问题。在不深入了解细节的情况下,问题是旧的PHP版本(从源代码构建)与包管理器中的更新的cURL和OpenSSL库的组合导致了不兼容版本的冲突。

我最终解决问题的基本步骤是从发行版包管理器安装cURL和OpenSSL。使用它们可以下载兼容的cURL和OpenSSL版本的源代码包。卸载cURL。从源代码构建OpenSSL,然后构建cURL,配置为使用我刚刚构建的OpenSSL。在那里,我能够使用新构建的cURL和OpenSSL库来构建PHP。

这是我最后得到的安装脚本的片段:

代码语言:javascript
复制
# [curl should have been installed from package manager before this]
# Note: This script is based on Ubuntu 18. Paths may differ for different distributions. 

cd /usr/local/src

# Download OpenSSL and cURL sources first, using the pre-installed version of cURL. 
curl -SL --progress-bar https://www.openssl.org/source/old/1.0.2/openssl-1.0.2n.tar.gz -o openssl-1.0.2n.tar.gz
curl -SL https://curl.se/download/curl-7.65.0.tar.xz -o curl-7.65.0.tar.xz

# Remove the pre-installed cURL, we'll rebuild it from the source bundle,
# using the shared libraries from the OpenSSL version we will build now. 
apt-get remove -y --purge curl

# Extract and build OpenSSL from source package, making sure to enable the `shared` option
# so that `libssl.so` and `libcrypto.so` shared libraries are built, too. 
tar xvf openssl-1.0.2n.tar.gz
cd openssl-1.0.2n
./config shared # add the `-d` flag to include debug symbols
make -j 10
make install_sw

# Add a new config file for the dynamic linker, so that it knows to look for the new shared libraries we're building. 
# The first line is for where the new cURL libs will be, and the latter is where new OpenSSL libs will be.
echo -e "/usr/local/lib \n/usr/local/ssl/lib" > /etc/ld.so.conf.d/local.conf

# Reload the linker cache
ldconfig

cd /usr/local/src

# Extract and build cURL from source package, making sure to tell it to use the OpenSSL version we just built. 
tar -xvf curl-7.65.0.tar.xz
cd curl-7.65.0
./configure --with-ssl=/usr/local/ssl # add the `--enable-debug` flag to include debug symbols
make -j 10
make install

# Reload the linker cache
ldconfig

cd /usr/local/src

# Reload utility (bin) locations; find the newly built `curl` binary. 
hash -r

curl -SL --progress-bar http://www.php.net/distributions/php-5.5.38.tar.bz2 -o php-5.5.38.tar.bz2
tar -xvf php-5.5.38.tar.bz2
cd php-5.5.38

# Make sure the `--with-openssl` flag includes a path to the SSL libs created after building it from source. 
# `--with-curl`, by default, will look in `/usr/local/lib` and `/usr/local/include`; When built from source, 
# the generated libraries WILL be placed here, so you shouldn't need to specify the path, unless you put them elsewhere
# or your distro uses different paths
./configure \
    --with-openssl=/usr/local/ssl \ 
    --with-curl \
    # Any other config flags here
    # Other flags were omitted for this example

make -j 10
make install

注意:我写这个是为了一个测试和开发环境。即使忽略它使用的是PHP5.5,这也可能不适合于生产环境。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17439199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档