0

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?

wnull
  • 217
  • 6
  • 21

1 Answers1

1

Try this, What is the easiest way to use the HEAD command of HTTP in PHP?

you will get HTTP Response Code without BODY. It will save time

Labradorcode
  • 381
  • 3
  • 15
  • 1
    Link-only answers are generally [frowned upon](http://meta.stackexchange.com/a/8259/204922) on Stack Overflow. In time it is possible for links to atrophy and become unavailable, meaning that your answer is useless to users in the future. It would be best if you could provide the general details of your answer in your actual post, citing your link as a reference. – Federico klez Culloca Nov 14 '19 at 11:49
  • 1
    Thanks and sorry, my bad. – Labradorcode Nov 14 '19 at 11:55