PHP Curl Connections fails without Error

Job ID: 33187534

Budget: $10 – $20 USD

Please read carefully my question because I have been searching this issue more than 1 hour and i didn't find a correct answer.

I have five dedicated servers (I have root access and those servers are always 100% up).
In my main server I have this script

function curl_get($url){

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // Number of secs to wait while trying to connect. Use 0 to wait indefinitely.
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_STDERR, fopen('php://stderr', 'w'));


$server_output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);


if ($httpcode == 200 || $httpcode == 301){
return $server_output;
} else if ($httpcode == 404){
return 404;
} else if ($httpcode == 403){
return 403;
} else {
return 607; // No response or Error on server
}

}

What i do is select randomly one ip server of the four servers that it left me, and then i call this function which is hosted in the main server like this:

echo curl_get('http://' . $randomIPFromMyFourServers[0] . '/something.php');
echo curl_get('http://' . $randomIPFromMyFourServers[1] . '/something.php');
echo curl_get('http://' . $randomIPFromMyFourServers[2] . '/something.php');
echo curl_get('http://' . $randomIPFromMyFourServers[3] . '/something.php');

So here what i am doing is calling this function 'curl_get()' four times and every time when it was called, the url was different (I mean in different order) but not repeated.

So the issue what i am facing it's that sometimes i get a response from the server, not always. The first thing that it comes to our mind when we try to explain what it's happening here it's that the Loadbalancer has blocked our response or the '$httpcode' it was different to '200 or 301'. Well, i have inspected the logs from the LoadBalancer and I discovered that they never received the request. So the problem it's from the main server, being more precesily with our 'script function curl_get'.

Looking forward on internet, I have found that a guy in Stackoverflow [Why does a cURL connection fail (without error) if no timeout is set?][1] had the same problem with PHP and CURL. The solution in that post was if you set 'CURLOPT_RETURNTRANSFER' to 'FALSE' it works. I have tried it and it's right with 'CURLOPT_RETURNTRANSFER' to 'FALSE' fix my scripts.

However, setting 'CURLOPT_RETURNTRANSFER' to 'FALSE' it doesn't capture the response, what it does is printing it. So this is not a definitely solution.


[1]: https://stackoverflow.com/questions/36179796/why-does-a-curl-connection-fail-without-error-if-no-timeout-is-set
Related categories: PHP JavaScript Linux Software Architecture Shell Script