I have a little code to find the right server. About it: there is a large array with links to the server. There is only one valid server (or none at all). The script goes through them in a loop, finds the right one, shifting and checking the HTTP code (valid 200
, invalid 400
).
Code:
$r = false;
$urls = ['url1', 'url2', /* ... */];
for ($i = 0; $i <= count($urls); $i++)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urls[$i]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$f = curl_exec($ch);
if (!curl_errno($ch))
{
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code === 200) {
$r = true;
echo $f;
break;
} else {
continue;
}
}
curl_close($ch);
}
if ($r === false) return 'not found';
And the problem is that the servers give out huge json data and the script slows down and it takes a lot of time. Are there any alternative approaches for finding the right server, in order to save time?