cURL 在 CLI 中有效,但在 PHP 中无效?
cURL works in CLI, but not in PHP?
这是我使用的代码:
curl -k https://www.ashleydirect.com/graphics/ad_images/T908-6.jpg
这工作正常(“-k”标志是工作或超时所必需的)
然后我在 PHP 中使用此代码:
$ch = curl_init("https://www.ashleydirect.com/graphics/ad_images/T908-6.jpg");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
它超时了——我尝试了很多变体,但 $result
总是 false.
这是我执行 phpinfo():
时的 PHP cURL 信息
cURL support enabled
cURL Information 7.38.0
Age 3
Features
AsynchDNS No
Debug No
GSS-Negotiate No
IDN Yes
IPv6 Yes
Largefile Yes
NTLM Yes
SPNEGO No
SSL Yes
SSPI No
krb4 No
libz Yes
CharConv No
Protocols dict, file, ftp, ftps, gopher, http, https, imap, imaps, pop3, pop3s, rtsp, smtp, smtps, telnet, tftp
Host x86_64-unknown-linux-gnu
SSL Version OpenSSL/1.0.1e
ZLib Version 1.2.3
如有任何想法,我们将不胜感激。
更新
这是来自curl_getinfo($ch)
的信息:
array (
'url' => 'https://www.ashleydirect.com/graphics/ad_images/T908-6.jpg',
'content_type' => NULL,
'http_code' => 0,
'header_size' => 0,
'request_size' => 0,
'filetime' => -1,
'ssl_verify_result' => 1,
'redirect_count' => 0,
'total_time' => 59.27538100000000298450686386786401271820068359375,
'namelookup_time' => 0.00975999999999999957867036215475309290923178195953369140625,
'connect_time' => 0.05170500000000000095923269327613525092601776123046875,
'pretransfer_time' => 0,
'size_upload' => 0,
'size_download' => 0,
'speed_download' => 0,
'speed_upload' => 0,
'download_content_length' => -1,
'upload_content_length' => -1,
'starttransfer_time' => 0,
'redirect_time' => 0,
'certinfo' =>
array (
),
'primary_ip' => '65.207.240.29',
'primary_port' => 443,
'local_ip' => '172.24.32.132',
'local_port' => 54461,
'redirect_url' => '',
)
更新 2
来自curl_error
的回复:
Unknown SSL protocol error in connection to www.ashleydirect.com:443
更新 3 - 解决方案
我想清楚地说明我想出的解决方案,感谢@Valery Viktorovsky 指出他们只接受 TLS 1.0。
解决方案是添加:
// Set to TLS 1.0 (CURL_SSLVERSION_TLSv1_0)
curl_setopt($ch, CURLOPT_SSLVERSION, 4);
您的 php 代码没问题。在 curl_exec
之后执行 curl_getinfo($ch)
和 curl_error($ch)
以查看服务器返回的响应代码。
更新:我测试了 ashleydirect.com SSL 证书,它只支持 TLS 1.0。因此,请确保您的 php 版本支持 TLSv1.0。
它对我来说工作正常,没有 -k
的命令行和 PHP 代码。
如果由于某种原因超时,您可以设置更大的超时时间:
// if it times out on establishing the connection
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // seconds
// if it times out while waiting for the response
curl_setopt($ch, CURLOPT_TIMEOUT, 60); // seconds
另外,调用curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false)
通常伴随着:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
得到完整的效果。
这是我使用的代码:
curl -k https://www.ashleydirect.com/graphics/ad_images/T908-6.jpg
这工作正常(“-k”标志是工作或超时所必需的)
然后我在 PHP 中使用此代码:
$ch = curl_init("https://www.ashleydirect.com/graphics/ad_images/T908-6.jpg");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
它超时了——我尝试了很多变体,但 $result
总是 false.
这是我执行 phpinfo():
时的 PHP cURL 信息cURL support enabled
cURL Information 7.38.0
Age 3
Features
AsynchDNS No
Debug No
GSS-Negotiate No
IDN Yes
IPv6 Yes
Largefile Yes
NTLM Yes
SPNEGO No
SSL Yes
SSPI No
krb4 No
libz Yes
CharConv No
Protocols dict, file, ftp, ftps, gopher, http, https, imap, imaps, pop3, pop3s, rtsp, smtp, smtps, telnet, tftp
Host x86_64-unknown-linux-gnu
SSL Version OpenSSL/1.0.1e
ZLib Version 1.2.3
如有任何想法,我们将不胜感激。
更新
这是来自curl_getinfo($ch)
的信息:
array (
'url' => 'https://www.ashleydirect.com/graphics/ad_images/T908-6.jpg',
'content_type' => NULL,
'http_code' => 0,
'header_size' => 0,
'request_size' => 0,
'filetime' => -1,
'ssl_verify_result' => 1,
'redirect_count' => 0,
'total_time' => 59.27538100000000298450686386786401271820068359375,
'namelookup_time' => 0.00975999999999999957867036215475309290923178195953369140625,
'connect_time' => 0.05170500000000000095923269327613525092601776123046875,
'pretransfer_time' => 0,
'size_upload' => 0,
'size_download' => 0,
'speed_download' => 0,
'speed_upload' => 0,
'download_content_length' => -1,
'upload_content_length' => -1,
'starttransfer_time' => 0,
'redirect_time' => 0,
'certinfo' =>
array (
),
'primary_ip' => '65.207.240.29',
'primary_port' => 443,
'local_ip' => '172.24.32.132',
'local_port' => 54461,
'redirect_url' => '',
)
更新 2
来自curl_error
的回复:
Unknown SSL protocol error in connection to www.ashleydirect.com:443
更新 3 - 解决方案
我想清楚地说明我想出的解决方案,感谢@Valery Viktorovsky 指出他们只接受 TLS 1.0。
解决方案是添加:
// Set to TLS 1.0 (CURL_SSLVERSION_TLSv1_0)
curl_setopt($ch, CURLOPT_SSLVERSION, 4);
您的 php 代码没问题。在 curl_exec
之后执行 curl_getinfo($ch)
和 curl_error($ch)
以查看服务器返回的响应代码。
更新:我测试了 ashleydirect.com SSL 证书,它只支持 TLS 1.0。因此,请确保您的 php 版本支持 TLSv1.0。
它对我来说工作正常,没有 -k
的命令行和 PHP 代码。
如果由于某种原因超时,您可以设置更大的超时时间:
// if it times out on establishing the connection
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // seconds
// if it times out while waiting for the response
curl_setopt($ch, CURLOPT_TIMEOUT, 60); // seconds
另外,调用curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false)
通常伴随着:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
得到完整的效果。