php 我如何找出 cURL 挂起且无响应的原因?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2828559/
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
How can I figure out why cURL is hanging and unresponsive?
提问by SenorPuerco
I am trying to track down an issue with a cURL call in PHP. It works fine in our test environment, but not in our production environment. When I try to execute the cURL function, it just hangs and never ever responds. I have tried making a cURL connection from the command line and the same thing happens.
我正在尝试使用 PHP 中的 cURL 调用来追踪问题。它在我们的测试环境中运行良好,但在我们的生产环境中却没有。当我尝试执行 cURL 函数时,它只是挂起并且永远不会响应。我试过从命令行建立一个 cURL 连接,同样的事情发生了。
I'm wondering if cURL logs what is happening somewhere, because I can't figure out what is happening during the time the command is churning and churning. Does anyone know if there is a log that tracks what is happening there?
我想知道 cURL 是否记录了某处发生的事情,因为我无法弄清楚在命令搅动和搅动期间发生了什么。有谁知道是否有日志记录那里发生的事情?
I think it is connectivity issues, but our IT guy insists I should be able to access it without a problem. Any ideas? I'm running CentOS and PHP 5.1.
我认为这是连接问题,但我们的 IT 人员坚持认为我应该能够毫无问题地访问它。有任何想法吗?我正在运行 CentOS 和 PHP 5.1。
Updates:Using verbose mode, I've gotten an error 28 "Connect() Timed Out". I tried extending the timeout to 100 seconds, and limiting the max-redirs to 5, no change. I tried pinging the box, and also got a timeout. So I'm going to present this back to IT and see if they will look at it again. Thanks for all the help, hopefully I'll be back in a half-hour with news that it was their problem.
更新:使用详细模式,我收到错误 28“Connect() Timed Out”。我尝试将超时延长至 100 秒,并将 max-redirs 限制为 5,没有变化。我尝试 ping 盒子,但也超时了。所以我将把它提交给 IT,看看他们是否会再次查看它。感谢所有的帮助,希望我能在半小时后回来,告诉他们这是他们的问题。
Update 2:Turns out my box was resolving the server name with the external IP address. When IT gave me the internal IP address and I replaced it in the cURL call, everything worked great. Thanks for all the help everybody.
更新 2:原来我的盒子正在用外部 IP 地址解析服务器名称。当 IT 给了我内部 IP 地址并且我在 cURL 调用中替换它时,一切都运行良好。感谢大家的帮助。
采纳答案by Adam Hopkinson
In your php, you can set the CURLOPT_VERBOSE variable:
在您的 php 中,您可以设置 CURLOPT_VERBOSE 变量:
curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
This then logs to STDERR, or to the file specified using CURLOPT_STDERR(which takes a file pointer):
然后记录到 STDERR 或使用指定的文件CURLOPT_STDERR(它需要一个文件指针):
curl_setopt($curl, CURLOPT_STDERR, $fp);
From the command line, you can use the following switches:
从命令行,您可以使用以下开关:
--verboseto report more info to the command line--trace <file>or--trace-ascii <file>to trace to a file
--verbose向命令行报告更多信息--trace <file>或--trace-ascii <file>追踪到一个文件
You can use --trace-timeto prepend time stamps to verbose/file outputs
您可以使用--trace-time将时间戳添加到详细/文件输出
回答by Ramesh Tabarna
You can also use curl_getinfo() to get information about your specific transfer.
您还可以使用 curl_getinfo() 获取有关您的特定传输的信息。
回答by Matthew Groves
Have you tried setting CURLOPT_MAXREDIRS? I've found that sometimes there will be an 'infinite' redirect loop for some websites that a normal browser user doesn't see.
您是否尝试过设置 CURLOPT_MAXREDIRS?我发现,对于某些普通浏览器用户看不到的网站,有时会出现“无限”重定向循环。
回答by Pekka
If at all possible, try sudoing as the user PHP runs under (possibly the one Apache runs under).
如果可能,请尝试sudo以 PHP 运行的用户身份(可能是 Apache 运行的用户)。
The curlproblem could have various reasons that require a user input, for example an untrusted certificate that is stored in the trusted certificates cache of the root user, but not the PHP one. In that case, the command would be waiting for an input that never happens.
该curl问题可能有多种需要用户输入的原因,例如存储在 root 用户的可信证书缓存中的不受信任证书,而不是 PHP 证书。在这种情况下,该命令将等待一个永远不会发生的输入。
Update:This applies only if you run curl externally using exec- maybe it doesn't apply.
更新:这仅适用于您在外部使用 curl 运行时exec- 也许它不适用。

