How to catch cURL errors in PHP
You can use the curl_error()
function to detect if there was some error. For example:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $your_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true); // Required for HTTP error codes to be reported via our call to curl_error($ch)
//...
curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
}
curl_close($ch);
if (isset($error_msg)) {
// TODO - Handle cURL error accordingly
}
See the description of libcurl error codes here
See the description of PHP curl_errno() function here
See the description of PHP curl_error() function here
Not getting any error message or content from cURL
Per the curl_exec()
documentation:
Returns TRUE on success or FALSE on failure. However, if the
CURLOPT_RETURNTRANSFER
option is set, it will return the result on success, FALSE on failure.
Your call to var_dump($content);
is showing bool(false)
, which means curl_exec()
is failing. That is why you are not getting any response content. Use curl_errno()
and/or curl_error()
to find out why it is failing.
One thing I do notice is curl_getinfo()
is reporting [redirect_count] => 20
. That is a lot of redirects. The server is likely getting stuck in an endless redirect loop and curl_exec()
decides to fail after awhile. See CURLOPT_MAXREDIRS
. Check if the error number being reported is CURLE_TOO_MANY_REDIRECTS
.
Catching cURL errors with PHP/Laravel
Figured it out. Guzzle has its own error handling for requests.
Source: http://docs.guzzlephp.org/en/stable/quickstart.html#exceptions
Solution:
use GuzzleHttp\Exception\RequestException;
...
try {
$crawler = $goutteClient->request('GET', 'https://www.google.com');
$crawlerError = false;
} catch (RequestException $e) {
$crawlerError = true;
}
if ($crawlerError == true) {
do the thing
} else {
do the other thing
}
Curl is working however error with php CURL
To avoid «500 error» (for example) be sure to:
set proper "Referer: " header if needed, with
curl_setopt(CURLOPT_REFERER, 'ref page');
set proper "User-Agent: " header, with
curl_setopt(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
PHP curl: check response for error before saving to file
Do a curl_getinfo() before writing to the file (and don't open it in the beginning of the code).
Example:
$ch = curl_init('http://www.google.com/');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$c = curl_exec($ch);
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200)
echo "Something went wrong!";
Any response code not 200 (success) is considered an error, the code above will most likely not return anything as google.com is up and online ;)
Laravel - Correct way to catch cURL exceception
Unlike Guzzle, Laravel's HttpClient does not throw errors if the response is > 400
.
You should simply use an if statement to check the response status code. See: https://laravel.com/docs/8.x/http-client#error-handling
You can call use the following checks:
// Determine if the status code is >= 200 and < 300...
$response->successful();
// Determine if the status code is >= 400...
$response->failed();
// Determine if the response has a 400 level status code...
$response->clientError();
// Determine if the response has a 500 level status code...
$response->serverError();
So in your case you can simply do something like this:
$response = Http::timeout(2)->asForm()->post('https://' . $this->ip_address, [
'username' => $this->username,
'password' => $this->password
]);
if ($response->failed()) {
return view('your-view')->with([
'message' => 'Failed.',
]);
}
Related Topics
What's the Difference Between Isset() and Array_Key_Exists()
Deny Direct Access to a Folder and File by Htaccess
PHP Error: Fatal Error: Constant Expression Contains Invalid Operations
Bulk Insertion in Laravel Using Eloquent Orm
Execute PHP Script in Cron Job
How to Check If a Date Is in a Given Range
How to Turn Off Magic Quotes on Shared Hosting
PHP Fatal Error: Call to Undefined Function Curl_Init()
Codeigniter: How to Connect to Your Database Server Using the Provided Settings Error Message
Are Arrays in PHP Copied as Value or as Reference to New Variables, and When Passed to Functions
Streaming a Large File Using PHP
PHP Warning: PHP Startup: How to Load Dynamic Library
Warning: MySQLi_Select_Db() Expects Exactly 2 Parameters, 1 Given
How to Parse a CSV File Using PHP
PHP Simplexml How to Save the File in a Formatted Way
Are PHP Include Paths Relative to the File or the Calling Code