简体   繁体   中英

Checking Multi URL status

I want a lot of domain address for checking the status. I try multi curl but its to slow

class BotCronJobs extends Controller {

    public function __construct() {
    }

    public function index() {
        $Query = servers::all();    

        $urls = [];
        foreach ($Query as $item){
            $urls[$item->id] = $item->serverUrl;
        }
        var_dump($this->test($urls));
    }

    public function test($urls = []) {

        $status = [];
        $mh = curl_multi_init();
        foreach($urls as $key => $value){
            $ch[$key] = curl_init($value);
            curl_setopt($ch[$key], CURLOPT_NOBODY, true);
            curl_setopt($ch[$key], CURLOPT_HEADER, true);
            curl_setopt($ch[$key], CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch[$key], CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch[$key], CURLOPT_SSL_VERIFYHOST, false);
            curl_multi_add_handle($mh,$ch[$key]);
        }

        do {
            curl_multi_exec($mh, $running);
            curl_multi_select($mh);
        } while ($running > 0);

        foreach(array_keys($ch) as $key){
            $status[$key][] = curl_getinfo($ch[$key], CURLINFO_HTTP_CODE);
            $status[$key][] = curl_getinfo($ch[$key], CURLINFO_EFFECTIVE_URL);
            curl_multi_remove_handle($mh, $ch[$key]);
        }

        curl_multi_close($mh);

        return $status;

    }
}

I just need to check is server online or not and every server have an id it's important for me to understand which server is offline. is there any faster way?

your function looks nearly optimal to me. i guess its slow because some domains are not responding, and the default CURLOPT_CONNECTTIMEOUT is too high. try setting CURLOPT_CONNECTTIMEOUT to 1 and CURLOPT_TIMEOUT to 2, that should make it stall for max 2 seconds on not-responding domains.

also, if you don't actually need the http response code, but is ok with just checking if the server is actually accepting connections or not, maybe using the socket_ api would be faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM