Monday, 13 January 2014

cURL timeout problem and solution

One page I made uses cURL to scrape small data from external web page. The problem I faced was when the page was offline for a while, my page did not load.

The fix I stumbled into was to add these three lines to my code.
 curl_setopt($ch, CURLOPT_TIMEOUT_MS, 5000);
 $curl_errno = curl_errno($ch);
 $curl_error = curl_error($ch);
 
Now my page still runs when the remote page is down and I get a error message.
 $url = $_GET['url'];
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 /* set timeout in ms */
 curl_setopt($ch, CURLOPT_TIMEOUT_MS, 5000);
 $data = curl_exec($ch);
 $curl_errno = curl_errno($ch);
 $curl_error = curl_error($ch);
 curl_close($ch);

 if ($curl_errno > 0) {
  echo "cURL Error ($curl_errno): $curl_error\n";
  } else {
  echo $match[0][0]);
  }