I've done every bit of tips and answers but I can't seem to find the right solution.
my cURL is as follows
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $curlIP."api/sparts/search/");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "key=".$q."&page=".$pg);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($curl);
curl_close ($curl);
What does this do?
It gets products data from a server where $q
is the search key and $pg
is the page number
This cURL works as intended assuming the server will always have an internet connection. As the developer we needed to have an error message whenever the cURL cannot connect to the server.
What is the problem?
For me to actually set an error message I need to know first if the error has happened. This where it gets weird for me. I tried to simulate connection failure by disabling the internet adapter of the server.
The result is:
the page stops parsing everything after the $data = curl_exec($curl);
This includes the footer and everything after that line. I knew it because I tried to put echoes before and after curl_exec
and it really stops there. therefore I cannot catch the error itself.
What have I tried?
I tried
- PHP
curl_error()
- which didn't really help since the script stops atcurl_exec
- PHP
curl_errno()
- same - try and catch - This should have catch the error but it didn't which frustrates me even more.