windows 为什么 Curl 会比 Web 浏览器慢?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3669923/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:12:33  来源:igfitidea点击:

Why could Curl be slower than a web browser?

phpwindowscurlwindows-xp

提问by jarkam

I am using this classto make one GET and another POST request to a website (the first request is to set a cookie). I am testing in a Win XP virtual machine with virtualbox, using wamp from wampserver dot com. The 2 requests takes from 10 to 18 seconds (with curl), but if I make those request directly via the webbrowser in that same virtual machine the website loads in just a few seconds, and it retrieves all the images, css, etc.

我正在使用这个类向网站发出一个 GET 和另一个 POST 请求(第一个请求是设置一个 cookie)。我正在使用来自 wampserver dot com 的 wamp 在带有 virtualbox 的 Win XP 虚拟机中进行测试。这 2 个请求需要 10 到 18 秒(使用 curl),但是如果我直接通过同一虚拟机中的 webbrowser 发出这些请求,则网站只需几秒钟即可加载,并且它会检索所有图像、css 等。

What could be causing curl to work so slow? is there a way to fix it?

是什么导致 curl 工作如此缓慢?有办法解决吗?

采纳答案by mmonem

Check your web server logs and try to find any difference between the requests from the normal web browser and the requests from curl

检查您的 Web 服务器日志并尝试找出来自普通 Web 浏览器的请求和来自 curl 的请求之间的任何差异

回答by Frankie

Curl is probably trying to Reverse DNS the server and, as it cant, it just hangs there a little waiting for the timeout.

Curl 可能正在尝试对服务器进行反向 DNS,因为它不能,它只是挂在那里等待超时。

If the timeout is given by IPV6 you can try CURL_IPRESOLVE_V4to bypass it altogether. It really depends on your machine configuration and is more a question to Server Fault.

如果超时是由 IPV6 给出的,您可以尝试CURL_IPRESOLVE_V4完全绕过它。这实际上取决于您的机器配置,并且对 Server Fault 来说更像是一个问题。

回答by Tarun

I faced the same issue, using the curl command.

我使用 curl 命令遇到了同样的问题。

as suggested above - forcing ipv4 only dns lookup fixed it.

如上所述 - 强制 ipv4 仅 dns 查找修复了它。

curl -4 $url# nice and fast

curl -4 $url# 又好又快

(I already had ::1 localhostin my hosts file - but that didn't help).

::1 localhost我的主机文件中已经有了- 但这没有帮助)。

回答by whuhacker

It is probably due to IPv6.

这可能是由于 IPv6。

Try add

尝试添加

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

回答by lfender6445

I ran into this issue with a local web server. I was able to fix by adding

我在本地 Web 服务器上遇到了这个问题。我能够通过添加来修复

::1 localhost

to /etc/hosts/file.

/etc/hosts/文件中。

This is the ipv6 notation for 127.0.0.1

这是127.0.0.1ipv6 表示法