title: PHP Catch cURL CURLOPT_TIMEOUT and CURLOPT_CONNECTTIMEOUT events and take action
body: <p>I want to detect, catch and do something when CURLOPT_CONNECTTIMEOUT and CURLOPT_TIMEOUT are detected.</p>
  
  <p>How do i do this ?</p>
  
  <p>I have the following headers:</p>
  
  <pre><code>public static $userAgents = array(
      'FireFox3' =&gt; 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0',
      'GoogleBot' =&gt; 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
      'IE7' =&gt; 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
      'Netscape' =&gt; 'Mozilla/4.8 [en] (Windows NT 6.0; U)',
      'Opera' =&gt; 'Opera/9.25 (Windows NT 6.0; U; en)'
  );
  public static $options = array(
      CURLOPT_USERAGENT =&gt; 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
      CURLOPT_AUTOREFERER =&gt; true,
      CURLOPT_FOLLOWLOCATION =&gt; true,
      CURLOPT_RETURNTRANSFER =&gt; true,
      CURLOPT_FRESH_CONNECT =&gt; true,
      CURLOPT_COOKIEJAR =&gt; "cookies.txt",
      CURLOPT_COOKIEFILE =&gt; "cookies.txt",
      CURLOPT_SSL_VERIFYPEER =&gt; false,
      CURLOPT_CONNECTTIMEOUT =&gt; 5,
      CURLOPT_TIMEOUT =&gt; 300,
          //CURLOPT_COOKIESESSION =&gt; false,
  );
  </code></pre>
  
  <p>and the folowing function, where $res is a array that contains html file names</p>
  
  <pre><code>public function multiCurl($res, $options = "") {
  
      if (count($res) &lt;= 0)
          return False;
  
      $handles = array();
  
      if (!$options) // add default options
          $options = self::$options;
  
      // add curl options to each handle
      foreach ($res as $k =&gt; $row) {
          $ch{$k} = curl_init();
          $options[CURLOPT_URL] = $row['url'];
          curl_setopt_array($ch{$k}, $options);
          $handles[$k] = $ch{$k};
      }
  
      $mh = curl_multi_init();
  
      foreach ($handles as $k =&gt; $handle) {
          curl_multi_add_handle($mh, $handle);
      }
  
      $running_handles = null;
      //execute the handles
      do {
          $status_cme = curl_multi_exec($mh, $running_handles);
      } while ($cme == CURLM_CALL_MULTI_PERFORM);
  
      while ($running_handles &amp;&amp; $status_cme == CURLM_OK) {
          if (curl_multi_select($mh) != -1) {
              do {
                  $status_cme = curl_multi_exec($mh, $running_handles);
              } while ($status == CURLM_CALL_MULTI_PERFORM);
          }
      }
  
      foreach ($res as $k =&gt; $row) {
          $res[$k]['error'] = curl_error($handles[$k]);
          if (!empty($res[$k]['error']))
              $res[$k]['data'] = '';
          else {
              //$res[$k]['data'] = curl_multi_getcontent($handles[$k]);  // get results 
              file_put_contents(CRAWLER_FILES . $k . '.html', curl_multi_getcontent($handles[$k]));
          }
  
          // close current handler
          curl_multi_remove_handle($mh, $handles[$k]);
      }
      curl_multi_close($mh);
      return $res; // return response
  }
  </code></pre>