Why does a cURL connection fail (without error) if no timeout is set?
<p>I have a PHP script that connects to an URL through cURL and then does something, depending on the returned HTTP status code:</p>

<pre><code>$ch = curl_init();
$options = array(
            CURLOPT_RETURNTRANSFER =&gt; true,
            CURLOPT_URL            =&gt; $url,
            CURLOPT_USERAGENT      =&gt; "What?!?"
);
curl_setopt_array($ch, $options);
$out = curl_exec($ch);
$code = curl_getinfo($ch)["http_code"];
curl_close($ch);

if ($code == "200") {
    echo "200";
} else {
   echo "not 200";
}
</code></pre>

<p>Some webservers are slow to reply, and <em>although the page is loaded in my browser after a few seconds</em> my script, when it tries to connect to that server, tells me that it did not receive a positive ("200") reply. So, apparently, the connection initiated by cURL timed out.</p>

<p>But why? I don't set a timeout in my script, and according to <a href="https://stackoverflow.com/questions/10308915/php-default-curl-timeout-value">other answers on this site</a> the default timeout for cURL is definitely longer than the three or four seconds it takes for the page to load in my browser.</p>

<p>So why does the connecion time out, and how can I get it to last longer, if, apparently, it is already set to infinite?</p>

<hr>

<p>Notes:</p>

<ul>
<li>The same URL doesn't always time out. So sometimes cURL <em>can</em> connect.</li>
<li>It is not one specific URL that sometimes times out, but different URLs at different times.</li>
<li>I'm on a shared server, so I don't have root access to any files.</li>
<li>I tried to look at <code>curl_getinfo($ch)</code> and <code>curl_error($ch)</code> – as per @drew010's suggestion in the comments – but both were empty whenever the problem happened.</li>
<li>The whole script runs for a little more than one minute. In this time it connects to 300+ URLs successfully. Even when one of the URLs fails, the other connections are successfully made. So the script does not time out.</li>
<li>cURL does not time out either, because when I try to connect to an URL with a script sleeping for 59 seconds, cURL successfully connects. So apparently the slowness of the failing URL is not a problem in itself for cURL.</li>
</ul>

<hr>

<p><strong>Update</strong></p>

<p>Following @Karlos' suggestion in his answer, I used:</p>

<pre><code>CURLOPT_VERBOSE        =&gt; 1,
CURLOPT_STDERR         =&gt; $curl_log
</code></pre>

<p>(using code from <a href="https://stackoverflow.com/a/14087761/1322720">this answer</a>) and found the following in <code>$curl_log</code> when an URL failed (URL and IP changed):</p>

<pre><code>* About to connect() to www.somesite.com port 80 (#0)
*   Trying 104.16.37.249... * connected
* Connected to www.somesite.com (104.16.37.249) port 80 (#0)
GET /wp_german/?feed=rss2 HTTP/1.1
User-Agent: myURL
Host: www.somesite.com
Accept: */*

* Recv failure: Connection reset by peer
* Closing connection #0
</code></pre>

<p>So, I have found the <em>why</em> – thank you @Karlos! – and apparently @Axalix was right and it is a network problem. I'll now follow suggestions given on this site for that kind of failure. Thanks to everyone for their help!</p>