Using try / catch with cURL in PHP

I thought I decided it, but I obviously did not, and I hoped that someone could shed light on what I was doing wrong. I am trying to get some of the PHP code to check a file on the server and based on the response, perform an action. The file is a simple text file labeled true or false. If the file exists or the return is "true", the script goes to the same URL. If it does not exist (ie, the Server is unavailable) or returns false, the script goes to the second URL. Below is a snippet of code that I have used so far.

$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 '<p class="response1">'.$response.'</p>'; 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 '<p class="response2">'.$fb.'</p>'; $response = $fb; } // If cURL was successful, $response should now be true, otherwise it will have stayed false. echo '<p class="response3">'.$response.'</p>'; 

This example here passes only when processing a file test. The contents of "test.htm" is either "true" or "false".

This may seem like a confusing way to do this, but the file is located on a third-party server (I have no control over this), and it needs to be checked, since a third-party provider may decide to force a switch to the second url when performing maintenance work on the first URL. So I can’t just check for the file.

I tested the local setup and it all behaves as expected. The problem occurs when there is no file. $ Fb = curl_exec ($ curl); not interrupted, it just returns an empty value. I thought that since this is on the IIS PHP server, the problem may be related to PHP on the server, but now I see this problem locally (on a Unix system).

If anyone can shed light on what I can do wrong, I would really appreciate it. There could also be a much shorter way to test this file, I could go a long way to do this, so really appreciate any help.

T

+6
source share
1 answer

cURL does not throw exceptions; it is a thin shell for the C library to which it is bound.

I modified your example to use a more correct template.

 $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; // curl_exec will not return true if you use this, it will instead return the request body $options[CURLOPT_TIMEOUT] = 10; // Preset $response var to false and output $fb = ""; $response = false;// don't quote booleans echo '<p class="response1">'.$response.'</p>'; $curl = curl_init(); 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); if($fb !== false) { echo '<p class="response2">'.$fb.'</p>'; $response = $fb; } // If cURL was successful, $response should now be true, otherwise it will have stayed false. echo '<p class="response3">'.$response.'</p>'; 
+20
source

Source: https://habr.com/ru/post/919511/


All Articles