PHP cURL方法在某些URL上超时,但命令行始终有效 [英] PHP cURL methods time out on some URLs, but command line always works

查看:271
本文介绍了PHP cURL方法在某些URL上超时,但命令行始终有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试对某些URL使用PHP的cURL方法时,它会超时。当我将命令行用于相同的URL时,它工作正常。

When I attempt to use PHP's cURL methods for SOME URLs, it times out. When I use the commandline for the same URL, it works just fine.

我正在使用AWS,并且有一个运行yum的php-55 apache库的t2.medium框。

I am using AWS and have a t2.medium box running the php-55 apache libraries from yum.

这是我的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;
}

所以这里的调用效果很好:

So here is call that works just fine:

curl('http://www.google.com');

这将返回google主页的数据。

And this returns the data for the homepage of google.

但是,我尝试另一个URL:

However, I try another URL:

curl('http://www.trulia.com/profile/agent-1391347/overview');

我在curllog中得到了它:

And I get this in the 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

如果我从命令行运行此命令:

If I run this from the command line:

curl -s www.trulia.com/profile/agent-1391347/overview

它立即返回(在1秒内),没有输出。这是预期的。但是,当我运行此代码时:

It IMMEDIATELY returns (within 1 second) with NO output. This is expected. However when I run this:

curl -sL www.trulia.com/profile/agent-1391347/overview

就像我想要的那样,它会正确返回页面。

It returns the page properly, just as I would want.

那么,我的卷发怎么了?

So, what is wrong with my curl?

PHP 5.5.20

PHP 5.5.20

这里是cURL位来自我的phpinfo():

Here is the cURL bit from my phpinfo():

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


推荐答案

我已经检查了您的函数 curl()看起来不错。无需更改功能中的任何内容。您需要做的只是传递URL,因为它作为参数,无需将 HTTPS 更改为 HTTP

I have checked your function curl() It seems fine. No need to change anything in the function. What should you need to do is just pass the URL as it is as parameter no need to change HTTPS to HTTP

curl('http://www.trulia.com/profile/agent-1391347/overview');

原因:

您已经告诉 curl 不要验证 SSL

curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false);

让我知道您是否需要任何解释。

Let me know if you need any explanation.

这篇关于PHP cURL方法在某些URL上超时,但命令行始终有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆