Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a try/catch with cURL in PHP

Tags:

php

curl

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.

$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, only goes through the handling of the file test. The content of "test.htm" is either "true" or "false".

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.

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).

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.

T

like image 552
tadywankenobi Avatar asked Jul 02 '12 16:07

tadywankenobi


People also ask

Does cURL throw exception?

cURL does not throw exceptions, it is a thin wrapper for the C library it binds to.

How do you catch errors in cURL?

If you want to fetch the error message, make sure you fetch it before you close the current cURL session or the error message will be reset to an empty string. This is returned if CURLOPT_FAILONERROR is set TRUE and the HTTP server returns an error code that is >= 400.

Can we use try catch in PHP?

The primary method of handling exceptions in PHP is the try-catch. In a nutshell, the try-catch is a code block that can be used to deal with thrown exceptions without interrupting program execution. In other words, you can "try" to execute a block of code, and "catch" any PHP exceptions that are thrown.

Can I use cURL in PHP?

cURL is a PHP extension that allows you to use the URL syntax to receive and submit data. cURL makes it simple to connect between various websites and domains.


1 Answers

cURL does not throw exceptions, it is a thin wrapper for the C library it binds to.

I've modified your example to use a more correct pattern.

$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>';
like image 174
Jonathan Cremin Avatar answered Oct 07 '22 01:10

Jonathan Cremin