php - CakePHP 3 Http Client timeout error handling

898

I'm trying to use the CakePHP 3 Http Client to check urls for their response status code (404, 301, 200 etc)

$http = new Client();
$response = $http->get($links[$i]['url'],[],['timeout' => '10']);
$links[$i]['http_status'] = $response->statusCode();

However, if I come across a url that timesout, the entire script fails. I'm having trouble figuring out how to add error handling so that if it does timeout, I can log it and move on.

Any ideas?

766

Answer

Solution:

The solution was to use try/catch. I had tried this initially but missed the \ before Exception on the catch

try {
    // code
} catch (\Exception $e) {
    // error
}

People are also looking for solutions to the problem: php - Codeigniter for-each loop with API result

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.