当我尝试将PHP的cURL方法用于某些URL时,它会超时.当我使用命令行作为相同的URL时,它工作得很好.
我正在使用AWS并且有一个运行来自yum的php-55 apache库的t2.medium盒子.
这是我的PHP代码:
function curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept-Language: en-us'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
$fh = fopen('/home/ec2-user/curllog', 'w');
curl_setopt($ch, CURLOPT_STDERR, $fh);
$a = curl_exec($ch);
curl_close($ch);
fclose($fh);
$headers = explode("\n",$a);
var_dump($headers);
var_dump($a);
exit;
return $result;
}
Run Code Online (Sandbox Code Playgroud)
所以这里的调用工作得很好:
curl('http://www.google.com');
Run Code Online (Sandbox Code Playgroud)
这将返回谷歌主页的数据.
但是,我尝试了另一个URL:
curl('http://www.trulia.com/profile/agent-1391347/overview');
Run Code Online (Sandbox Code Playgroud)
我在curllog中得到了这个:
[ec2-user@central Node]$ cat ../curllog
* Hostname was NOT found in DNS cache
* Trying 23.0.160.99...
* Connected to www.trulia.com (23.0.160.99) port 80 (#0)
> GET /profile/agent-1391347/overview HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36
Host: www.trulia.com
Accept: */*
Accept-Language: en-us
* Operation timed out after 10002 milliseconds with 0 bytes received
* Closing connection 0
Run Code Online (Sandbox Code Playgroud)
如果我从命令行运行它:
curl -s www.trulia.com/profile/agent-1391347/overview
Run Code Online (Sandbox Code Playgroud)
它立即返回(1秒内)NO输出.这是预料之中的.但是,当我运行这个时:
curl -sL www.trulia.com/profile/agent-1391347/overview
Run Code Online (Sandbox Code Playgroud)
它正确地返回页面,就像我想要的那样.
那么,我的卷发有什么问题?
PHP 5.5.20
这是我的phpinfo()中的cURL位:
curl
cURL support => enabled
cURL Information => 7.38.0
Age => 3
Features
AsynchDNS => Yes
CharConv => No
Debug => No
GSS-Negotiate => No
IDN => Yes
IPv6 => Yes
krb4 => No
Largefile => Yes
libz => Yes
NTLM => Yes
NTLMWB => Yes
SPNEGO => Yes
SSL => Yes
SSPI => No
TLS-SRP => No
Protocols => dict, file, ftp, ftps, gopher, http, https, imap, imaps, ldap, ldaps, pop3, pop3s, rtsp, scp, sftp, smtp, smtps, telnet, tftp
Host => x86_64-redhat-linux-gnu
SSL Version => NSS/3.16.2 Basic ECC
ZLib Version => 1.2.7
libSSH Version => libssh2/1.4.2
Run Code Online (Sandbox Code Playgroud)
我检查过你的功能curl()
看起来很好.无需更改功能中的任何内容.你应该需要做的仅仅是通过URL,因为它是作为参数没有必要改变HTTPS
,以HTTP
curl('http://www.trulia.com/profile/agent-1391347/overview');
原因:
你已经告诉过curl
要不要验证SSL
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
如果您需要任何解释,请告诉我.
详细输出显示明确的超时问题:
- 操作在10002毫秒后超时,接收到0个字节
这表示您的网络设置存在问题.这些更难找到,这可以在你自己的一端(例如在web服务器或PHP可执行文件的上下文中)或另一端.两个地方都可以在一定程度上扩展,但是服务器即使它们具有不同的请求头也接受这两个请求,因此更有可能这是与执行上下文相关的,这也是您通常描述它的方式.
检查有关通过PHP执行这些请求的安全性和其他网络层是否有任何限制.例如,如果您没有进入系统管理和故障排除,请尝试使用其他服务器映像.从问题中分享的内容来看,很难说究竟是什么导致了超时.
尝试增加以下行中的超时值:
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
Run Code Online (Sandbox Code Playgroud)
这些是非常短的超时值 - CURLOPT_TIMEOUT 特别限制了整个执行时间,尝试给出更大的值:
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1603 次 |
最近记录: |