I hit external data sources in my php script, but would like to give up if it takes a while. Something along the lines of:
<?php
try_for_500_ms {
load_data_from_external_database();
}
catch_if_took_longer_than_500ms {
echo 'Took too long to load data';
}
In other words, I want it to give up trying to load the external data after a set amount of time and continue the script. Any ideas how to implement something like that? Thanks in advance.
Assuming your external datasource is contacted over HTTP, you would simply set the timeout option of whatever HTTP library you’re using. Assuming cURL, you’d use:
Other APIs should have similar options.
There’s no such language construct in PHP. If your API does not have an analogous option, you’d have to get into forking the process and the parent killing the child after some timeout.