php CURL 和 HTTPS,“无法解析主机”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1341644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CURL and HTTPS, "Cannot resolve host"
提问by swt83
I'm trying to fetch the contents of a page using CURL. The page that is doing the fetching is https and the page it is trying to fetch is also https. I'm getting an error "Couldn't resolve host" with all of the settings I try.
我正在尝试使用 CURL 获取页面的内容。正在获取的页面是 https,它试图获取的页面也是 https。我尝试的所有设置都出现错误“无法解析主机”。
$c=curl_init();
curl_setopt($c, CURLOPT_URL,$url);
//curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:x.x.x) Gecko/20041107 Firefox/x.x");
curl_setopt ($c, CURLOPT_RETURNTRANSFER, TRUE);
//curl_setopt($c, CURLOPT_SSL_VERIFYPEER, TRUE);
//curl_setopt($c, CURLOPT_SSL_VERIFYHOST, TRUE);
curl_setopt($c, CURLOPT_HEADER, FALSE);
$html=curl_exec($c);
if($html === false) {
echo curl_error($c);
}
else {
echo 'Operation completed without any errors';
}
curl_close($c);
Any ideas?
有任何想法吗?
采纳答案by KB22
Maybe a DNS issue?
也许是DNS问题?
Try your URL against this code:
针对此代码尝试您的网址:
$_h = curl_init();
curl_setopt($_h, CURLOPT_HEADER, 1);
curl_setopt($_h, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($_h, CURLOPT_HTTPGET, 1);
curl_setopt($_h, CURLOPT_URL, 'YOUR_URL' );
curl_setopt($_h, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
curl_setopt($_h, CURLOPT_DNS_CACHE_TIMEOUT, 2 );
var_dump(curl_exec($_h));
var_dump(curl_getinfo($_h));
var_dump(curl_error($_h));
回答by Staplerfahrer
I found that CURL can decide to use IPv6, in which case it tries to resolve but doesn't get an IPv6 answer (or something to that effect) and times out.
我发现 CURL 可以决定使用 IPv6,在这种情况下,它尝试解析但没有得到 IPv6 答案(或类似的结果)并超时。
You can try the command line switch -4 to test this out.
您可以尝试使用命令行开关 -4 来测试这一点。
In PHP, you can configure this line by setting this:
在 PHP 中,您可以通过设置此行来配置:
curl_setopt($_h, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
回答by TecBeast
I had the same problem. Coudn't resolve google.com. There was a bug somewhere in php fpm, which i am using. Restarting php-fpm solved it for me.
我有同样的问题。无法解析 google.com。我正在使用的 php fpm 中某处存在错误。重新启动 php-fpm 为我解决了这个问题。
回答by Rick
Just a note which may be helpful- I was having this trouble with Apache on my laptop (which connects by wifi AFTER startup), and restarting the server (after connect) fixed the issue. I guess in my case this may be to do with apache starting offline and perhaps there noting that DNS lookups fail?
只是一个可能有用的说明 - 我在我的笔记本电脑上的 Apache 遇到了这个问题(它在启动后通过 wifi 连接),并重新启动服务器(连接后)解决了这个问题。我想在我的情况下,这可能与 apache 离线启动有关,并且可能注意到 DNS 查找失败?
回答by LewisEmilyR
There is a current bug in glibc on Ubuntu which can have this effect: https://bugs.launchpad.net/ubuntu/+source/glibc/+bug/1674733
Ubuntu 上的 glibc 中存在一个当前的错误,可以产生这种效果:https: //bugs.launchpad.net/ubuntu/+source/glibc/+bug/1674733
To resolve it, update libc and all related (Packages that will be upgraded: libc-bin libc-dev-bin libc6 libc6-dev libfreetype6 libfreetype6-dev locales multiarch-support) and restart the server.
要解决它,请更新 libc 和所有相关(将要升级的软件包:libc-bin libc-dev-bin libc6 libc6-dev libfreetype6 libfreetype6-dev locales multiarch-support)并重新启动服务器。
回答by Jigar7521
After tried all above, still can't resolved my issue yet. But got new solution for my problem.
经过以上所有尝试,仍然无法解决我的问题。但是为我的问题找到了新的解决方案。
At server where you are going to make a request, there should be a entry of your virtual host.
在您要发出请求的服务器上,应该有一个虚拟主机的条目。
sudo vim /etc/hosts
and insert
并插入
192.xxx.x.xx www.domain.com
192.xxx.x.xx www.domain.com
The reason if you are making request from server to itself then, to resolve your virtual host or to identify it, server would need above stuff, otherwise server won't understand your requesting(origin) host.
如果您从服务器向自身发出请求,那么要解析您的虚拟主机或识别它,服务器将需要上述内容,否则服务器将无法理解您的请求(源)主机。
回答by Prince
We need to add host security certificate to php.ini file. For local developement enviroment we can add cacert.pemin your local php.ini.
我们需要在 php.ini 文件中添加主机安全证书。对于本地开发环境,我们可以在本地 php.ini 中添加cacert.pem。
do phpinfo(); and file your php.ini path open and add uncomment ;curl.capath
做 phpinfo(); 并将您的 php.ini 路径文件打开并添加取消注释 ;curl.capath
curl.capath=path_of_your_cacert.pem
curl.capath=path_of_your_cacert.pem
回答by Picard
If you do it on Windows XAMPP/WAMP it probaly won't work as in my case.
如果你在 Windows XAMPP/WAMP 上这样做,它可能不会像我的情况一样工作。
I solved the problem setting up Laravel's Homestead/Vagrant solution to create my (Ubuntu) development environment - it has built-in: Nginx, PHP 5.6, PHP 7.3, PHP 7.2, PHP 7.1, MySQL, PostgreSQL, Redis, Memcached, Node... to name just a few.
我解决了设置 Laravel 的 Homestead/Vagrant 解决方案以创建我的 (Ubuntu) 开发环境的问题 - 它内置了:Nginx、PHP 5.6、PHP 7.3、PHP 7.2、PHP 7.1、MySQL、PostgreSQL、Redis、Memcached、Node.js .. 仅举几例。
See herefor info how to set up the environment - it's really worth the effort!
有关如何设置环境的信息,请参见此处- 这确实值得付出努力!
Laravel Homestead is an official, pre-packaged Vagrant box that provides you a wonderful development environment without requiring you to install PHP, a web server, and any other server software on your local machine. No more worrying about messing up your operating system! Vagrant boxes are completely disposable. If something goes wrong, you can destroy and re-create the box in minutes!
Laravel Homestead 是一个官方的、预打包的 Vagrant 盒子,它为你提供了一个美妙的开发环境,而无需你在本地机器上安装 PHP、Web 服务器和任何其他服务器软件。再也不用担心弄乱你的操作系统了!Vagrant 盒子是完全一次性的。如果出现问题,您可以在几分钟内销毁并重新创建盒子!
Then you can easily switch PHP versions or set up more virtual hosts, new databases just in seconds.
然后,您可以在几秒钟内轻松切换 PHP 版本或设置更多虚拟主机、新数据库。
回答by sunil
add yourlocalhost
ex. 127.0.0.1 cards.localhost
in the /etc/hosts directory.
Now restart apache server
添加您的本地主机
前。/etc/hosts 目录中的 127.0.0.1 card.localhost。现在重启apache服务器
回答by Muhammad Haroon Bashir
Your getting the error because you're probably doing it on your Local server environment. You need to skip the certificates check when the cURL call is made. For that just add the following options
您收到错误是因为您可能是在本地服务器环境中执行此操作。进行 cURL 调用时,您需要跳过证书检查。为此,只需添加以下选项
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);

