I'm loading links from "geolocation ipAPIs". If it takes time to load one, I try another and so on with all. How do you do that? For example, give it two seconds to load, if it doesn't load, I finish it and try the next one.
I put two functions that open them
(This is what the sites ask for to enter):
<?php
/*--------------link_1------------*/
$userIP = $_SERVER [ 'REMOTE_ADDR' ];
$apiURL = 'http://ip-api.com/json/'.$userIP;
$ch = curl_init($apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$apiResponse = curl_exec($ch);
curl_close($ch);
$ipData = json_decode($apiResponse, true);
echo "<pre>",print_r($ipData),"</pre>";
/*--------------link_2------------*/
echo $var_export ( unserialize ( file_get_contents ( 'http://www.geoplugin.net/php.gp?ip=' . $_SERVER [ 'REMOTE_ADDR' ] ) ) ) ;
?>
I tried giving the functions a 2 second timer and found the function: set_time_limit() but it closes the whole script at the end of the two seconds.
Can a function timer be made?
First; take a look at the documentation: http://www.php.net/manual/en/function.curl-setopt.php
CURLOPT_CONNECTTIMEOUT - Specifies the number of seconds to wait while trying to connect, use 0 to wait indefinitely.
CURLOPT_TIMEOUT - Specifies the maximum number of seconds curl can wait to run.
Also remember to lengthen the execution time of your script equivalently: