php curl 标头不从网站返回? [英] php curl headers do not return from website?
问题描述
我使用此命令从该特定页面取回标题.
I use this command to get headers back from this specific page.
curl -I https://www.gearbest.com/mobile-phones/pp_1686789.html?wid=1817324
我从终端运行上面的命令,我得到了这些结果...
i run the above command from terminal and i get these results...
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 不存在"!我究竟做错了什么?任何帮助表示赞赏.
So far so good. But when i try to implement in php using the code below i get "URL not exists" ! What am i doing wrong? Any help appreciated.
$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";
}
推荐答案
首先,您发布的代码是有效的.你不需要改变它.
First of all, the code you posted is valid. You don't need to alter it.
如果你只是和前任一起试试.https://www.google.com
它有效.
If you just try it with ex. https://www.google.com
it works.
问题在于您的 PHP 版本使用 LibreSSL 进行 php-curl HTTPS 连接.
The problem is that your version of PHP uses LibreSSL for php-curl HTTPS connections.
我在 macOS 上遇到了同样的问题,我的 PHP 版本使用了 OpenTransport.
I have the same issue on macOS where my version of PHP uses OpenTransport.
您要连接的网站需要以 LibreSSL 无法处理的方式建立 SSL 连接.
The website you're connecting to requires to establish a SSL connection in a way that LibreSSL is not able to handle.
您需要编译/安装 OpenSSL 并在已编译用于 php-curl 的 PHP 解释器上运行您的代码.
You need to compile/install OpenSSL and run your code on a PHP interpreter that has been compiled to use it for php-curl.
更新:
当您使用 macOS 时,您可以从源代码编译 PHP.谷歌搜索 macos compile php from source
将返回一个食谱"列表来做到这一点.
as you're on macOS you may compile PHP from source.
Googling for macos compile php from source
will return a list of "recipes" to do that.
如果您使用 brew 来编译 PHP(以及所有网络应用程序堆栈"),您可以在这里查看:
If you use brew to compile PHP (and all the "web applications stack") you may take a look here:
osx 10.10 Curl POST到 HTTPS 网址会出现 SSLRead() 错误
问题(和答案)还解决了与您问题中的问题类似的问题.
The question (and answer) also address a problem similar to the one in your question.
这篇关于php curl 标头不从网站返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!