Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP - curl_exec taking 100ms
    text
    copied!<p>My PHP project works by grabbing data from a WCF REStful web service with curl.</p> <p>However, for some reason the curl_exec function always takes around 100ms to return, even though the WCF web service is on the same machine, and the WCF service function being called only takes 3ms to execute.</p> <p>I've profiled the WCF service and the entire WCF stack accepts a HTTP request and returns the resultant XML (a tiny XML structure with 5 elements) in under 10ms.</p> <p>Below is my cURL set-up code:</p> <pre><code> $curl = curl_init($this-&gt;uri); curl_setopt($curl, CURLOPT_HEADER , true ); // return response HTTP headers in output curl_setopt($curl, CURLOPT_RETURNTRANSFER, true ); // return result instead of echoing curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // stop cURL from verifying the peer's certificate. This is set because by default (and right now) cURL does not trust /any/ certificate. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true ); // follow redirects, Location: headers (but does it really?) curl_setopt($curl, CURLOPT_MAXREDIRS , 10 ); // but dont redirect more than 10 times curl_setopt($curl, CURLOPT_CUSTOMREQUEST , $this-&gt;method ); if( isset( $this-&gt;username ) ) curl_setopt($curl, CURLOPT_USERPWD , $this-&gt;username . ':' . $this-&gt;password ); if( isset( $this-&gt;userAgent ) ) curl_setopt($curl, CURLOPT_USERAGENT, $this-&gt;userAgent); if( isset( $this-&gt;content ) ) { curl_setopt($curl, CURLOPT_POSTFIELDS, $this-&gt;content ); array_push( $this-&gt;headers, 'Content-Length: '. $this-&gt;contentLength ); if( isset( $this-&gt;contentType ) ) array_push( $this-&gt;headers, 'Content-Type: ' . $this-&gt;contentType ); } if( isset( $this-&gt;contentFile ) ) { curl_setopt($curl, CURLOPT_INFILE , $this-&gt;contentFile ); curl_setopt($curl, CURLOPT_INFILESIZE, $this-&gt;contentLength ); array_push( $this-&gt;headers, 'Content-Length: '. $this-&gt;contentLength ); array_push( $this-&gt;headers, 'Content-Type: ' . $this-&gt;contentType ); } if( count( $this-&gt;headers ) &gt; 0 ) curl_setopt($curl, CURLOPT_HTTPHEADER, $this-&gt;headers ); </code></pre> <p>The call to curl_exec happens immediately after this block of code, and it's the curl_exec call that I measure at 100ms consistently.</p> <p>It's important I get the load times of this PHP site down, and I can't understand why this happens.</p> <p>UPDATE:</p> <p>cURL's curl_exec always seems to take 100ms regardless of what is being requested, although very interestingly when I make a second curl_exec request (in the same PHP page execution) it completes a lot quicker - around 20ms.</p> <p>The mystery deepens.</p> <p>UPDATE 2:</p> <p>I realised that curl_getinfo() returns an associative-array containing timing information. When I dump the info from the first cURL request I see the following:</p> <pre><code>'total_time' =&gt; 0.125, 'namelookup_time' =&gt; 0.125, 'connect_time' =&gt; 0.125, 'pretransfer_time' =&gt; 0.125, 'starttransfer_time' =&gt; 0.125, </code></pre> <p>Whereas all subsequent requests are like this (or better):</p> <pre><code>'total_time' =&gt; 0.016, 'namelookup_time' =&gt; 0, 'connect_time' =&gt; 0, 'pretransfer_time' =&gt; 0, 'starttransfer_time' =&gt; 0.016, </code></pre> <p>It seems the delay is in namelookup, but I don't know why DNS lookup takes so long given that Windows (the server OS) already has the DNS information cached and other applications (desktop, ASP.NET, etc) can resolve the name in under 1ms, but cURL does it in 125ms.</p> <p>What's so special about cURL that it can't use the OS's DNS cache?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload