title: Using a try/catch with cURL in PHP
body: <p>I thought I had resolved this but I obviously haven't and was hoping someone might be able to shed some light on what I am doing wrong. I am trying to get a piece of PHP code to check a file on a server and based on the response, perform an action. The file is a simple text file which has either the word "true" or "false" written on it. If the file exists or the return is "true" the script goes to one url. If it doesn't exist (i.e. server is unavailable) or the return is "false", the script goes to a second url. The following is the snippet of code I have used up until now.</p>
  
  <pre><code>$options[CURLOPT_URL] = 'https://[domain]/test.htm';
  
  $options[CURLOPT_PORT] = 443;
  $options[CURLOPT_FRESH_CONNECT] = true;
  $options[CURLOPT_FOLLOWLOCATION] = false;
  $options[CURLOPT_FAILONERROR] = true;
  $options[CURLOPT_RETURNTRANSFER] = true;
  $options[CURLOPT_TIMEOUT] = 10;
  
  // Preset $response var to false and output
  $fb = "";
  $response = "false";
  echo '&lt;p class="response1"&gt;'.$response.'&lt;/p&gt;';
  
  try {
      $curl = curl_init();
      echo curl_setopt_array($curl, $options);
      // If curl request returns a value, I set it to the var here. 
      // If the file isn't found (server offline), the try/catch fails and var should stay as false.
      $fb = curl_exec($curl);
      curl_close($curl);
  }
  catch(Exception $e){
      throw new Exception("Invalid URL",0,$e);
  }
  
  if($fb == "true" || $fb == "false") {
      echo '&lt;p class="response2"&gt;'.$fb.'&lt;/p&gt;';
      $response = $fb;
  }
  
  // If cURL was successful, $response should now be true, otherwise it will have stayed false.
  echo '&lt;p class="response3"&gt;'.$response.'&lt;/p&gt;';
  </code></pre>
  
  <p>This example here, only goes through the handling of the file test. The content of "test.htm" is either "true" or "false".</p>
  
  <p>This may seem like a convoluted way of doing it, but the file is on a third party server (I have no control over this) and needs to be checked as the third party vendor may decide to force a failover to the second url, while doing maintenance work on the first url. Hence why I can't just check for the file's existance.</p>
  
  <p>I have tested on a local setup and it all behaves as expected. The problem arises when the file is not there. The $fb = curl_exec($curl); doesn't fail, it just returns an empty value. I had thought that, as this is on an IIS PHP server, the issue might be PHP related on the server, but I am now seeing this issue locally (on a Unix system).</p>
  
  <p>If anyone could shed any light on what I might be doing wrong, I'd really appreciate it. There may also be a much shorter way of testing this file, I could be going the long way around to do this, so really appreciate any help.</p>
  
  <p>T</p>