Shawn Tolidano
Shawn Tolidano

Reputation: 196

PHP cURL methods time out on some URLs, but command line always works

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.

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

Here is my PHP code:

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');

And this returns the data for the homepage of google.

However, I try another URL:

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

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

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

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

Upvotes: 11

Views: 2868

Answers (4)

hakre
hakre

Reputation: 197767

The verbose output shows a clear timeout problem:

  • Operation timed out after 10002 milliseconds with 0 bytes received

This signals a problem with your network setup. These are harder to locate, this can be on your own end (e.g. in context of the webserver or the PHP executable) or on the other end. Both places are possible to a certain extend, however the server accepts both requests even if they have different request headers, so it is more likely that this is execution context related which is also how you generally describe it.

Check if there are any restrictions on security and other networking layers regarding performing those requests via PHP. E.g. try a different server image if you're not so into system administration and trouble-shooting. From what is shared in your question, this is hard to say what exactly causes your timeout.

Upvotes: 5

Sahil Patel
Sahil Patel

Reputation: 1600

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');

Reason:

You already told curl to don't verify the SSL

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

Let me know if you need any explanation.

Upvotes: 9

Sagar V
Sagar V

Reputation: 12478

You have 2 VARIABLES

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

The first one, CURLOPT_CONNECTTIMEOUT is maximum amount of time allowed to make connection to the server`

You can disable it by setting it to 0.

That is

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);

But it is not a good method if you are in a production environment because it will never timeout.

Now CURLOPT_TIMEOUT

From PHP Documentation

The maximum number of seconds to allow cURL functions to execute.

Set it to some higher value

curl_setopt($ch, CURLOPT_TIMEOUT, 20); // 20 Seconds.

Upvotes: 3

Chris Stringer
Chris Stringer

Reputation: 135

Try increasing the timeout values in the following lines:

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

Those are pretty short timeout values - the CURLOPT_TIMEOUT specifically limits the entire execution time, try giving larger values:

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

Upvotes: 4

Related Questions