title: Google Places API calls are slow in PHP/cURL as compared to fetch browser, how to speed up?
body: <p>When I call a Google Places autocomplete API call (over https) from PHP using cURL, I find that it is much slower than fetching the same URL from a web browser: in PHP such a call typically takes around 200ms, while fetching the same URL in a web browser, according to Firebug, takes no more than 45ms. That's a slowdown by a factor 4 to 5. More accurately: the first call in the web browser can take up to 250ms, but subsequent fetches in the web browser, even when using different parameters, are fast.</p>
  
  <p>Note that both are tested from the same computer, so I don't think you can blame it on my network speed.</p>
  
  <p>Now when adding the round-trip from the mobile app to my server, such a delay is very noticeable, almost unacceptable.</p>
  
  <p>So my question is: what is the cause of this extra slowdown, and is there a way to remedy it?</p>
  
  <p>My first guess is that the SSL handshake and verification of Google API's certificate may be part of the problem, and which may be why the first fetch in the browser is equally slow. But whatever I try, I can't find a way to make it faster in PHP. I have thought of adding an explicit cache for the certificate, I don't even know if that is technically feasible.</p>
  
  <p>Update: demo code (you'll have to fill in your own API key):</p>
  
  <pre><code>&lt;?php
      $input = isset($_GET['input']) ? $_GET['input'] : 'vla';
      define('APIKEY', 'your api key');
      $params = array( 
          'input' =&gt; $input,
          'types' =&gt; '(regions)',
          'sensor' =&gt; 'false',
          'language' =&gt; 'nl',
          'key' =&gt; APIKEY,
      );
      $url = "https://maps.googleapis.com/maps/api/place/autocomplete/json?" . http_build_query($params);
      // Initialize session and set URL.
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      // Set so curl_exec returns the result instead of outputting it.
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      // accept any CA
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      $t = array(microtime(true));
      // Get the response
      $json = curl_exec($ch);    
      $t[] = microtime(true);
      // close the channel.
      curl_close($ch);
      echo '&lt;plaintext&gt;';
      echo $url, "\n";
      // show benchmark time
      printf("%s\t%0.6fs\n", 'Time', $t[sizeof($t)-1]-$t[0]);
      // show JSON
      echo $json;
  </code></pre>