php curl头文件不从网站返回



我使用这个命令从这个特定的页面获取标题。

curl -I https://www.gearbest.com/mobile-phones/pp_1686789.html?wid=1817324

我从终端运行上面的命令,得到了这些结果。。。

HTTP/2 200
content-type: text/html; charset=UTF-8
pragma: public
last-modified: Fri, 02 Nov 2018 18:15:13 GMT
gbcdnlang: en
access-control-allow-origin: *
access-control-allow-methods: GET, POST
ng-cache: HIT
x-edgeconnect-midmile-rtt: 36
x-edgeconnect-origin-mex-latency: 137
cache-control: public, max-age=60
expires: Fri, 02 Nov 2018 23:39:15 GMT
date: Fri, 02 Nov 2018 23:38:15 GMT
set-cookie: ORIGINDC=3;Domain=.gearbest.com;Path=/
set-cookie: ORIGINDCPC=3;Domain=.gearbest.com;Path=/
set-cookie: AKAM_CLIENTID=d51abedb38c60f8a5217ae15808769fc; expires=Mon, 31-Dec-2038 23:59:59 GMT; path=/; domain=.gearbest.com
vary: User-Agent

到目前为止还不错。但是当我尝试使用下面的代码在php中实现时,我会得到"URL不存在"!我做错了什么?感谢您的帮助。

$url1 = "https://www.gearbest.com/mobile-phones/pp_1686789.html?wid=1817324";
$curl1 = curl_init($url1);
curl_setopt($curl1, CURLOPT_NOBODY, true);
curl_setopt($curl1, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($curl1, CURLOPT_TIMEOUT, 5); //timeout in seconds

$result1 = curl_exec($curl1);
if ($result1 !== false)
{
$statusCode1 = curl_getinfo($curl1, CURLINFO_HTTP_CODE);
if ($statusCode1 == 404)
{
echo "URL Not Exists";
}
else
{
echo "URL Exists";
}
}
else
{
echo "URL not Exists";
}

首先,您发布的代码是有效的。你不需要改变它。

如果你只是用前https://www.google.com试试,它就行了。

问题是您的PHP版本使用LibreSSL进行PHP-curlHTTPS连接。

我在macOS上也有同样的问题,我的PHP版本使用OpenTransport

您要连接的网站需要以LibreSSL无法处理的方式建立SSL连接。

您需要编译/安装OpenSSL,并在已编译用于PHP-ccurl的PHP解释器上运行代码。


更新:

当你在macOS上时,你可以从源代码编译PHP。在谷歌上搜索macos compile php from source会返回一个"食谱"列表。

如果您使用brew编译PHP(以及所有"web应用程序堆栈"(,您可以查看以下内容:

osx 10.10卷曲POST到HTTPS url会导致SSLRead((错误

这个问题(和答案(还解决了一个与你的问题类似的问题。

尝试添加CURLOPT_SSL_VERIFYPEER选项,它通过SSL连接,但检查证书,如下所示:

<?PHP
$handle=curl_init('https://www.google.com');
curl_setopt($handle, CURLOPT_VERBOSE, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
$content = curl_exec($handle);
echo $content;
?>

我认为这不是你的问题。我认为当通过PHP请求时,他们会阻止某些内容。当您使用file_get_contents时,您会得到以下输出。

Warning: file_get_contents(): SSL: Operation timed out in /test.php on line 2
Warning: file_get_contents(https://www.gearbest.com/mobile-phones/pp_1686789.html?wid=1817324): failed to open stream: HTTP request failed!  in /test.php on line 2

当您在脚本中使用以下url时,它工作正常(https://www.test.de(。

我对你的剧本做了一些其他的测试,但每次我都会得到一个结果和你的预期结果。

最新更新