PHP get_headers() alternative

后端 未结 3 2036
走了就别回头了
走了就别回头了 2020-12-28 10:46

I need a PHP script that reads the HTTP response code for each URL request.

something like

$headers = get_headers($theURL);
return substr($headers         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-28 11:07

    Use HttpRequest if you can: http://de2.php.net/manual/en/class.httprequest.php

    $request = new HttpRequest("http://www.example.com/");
    $request->send();
    echo $request->getResponseCode();
    

    Or do it the hard way: http://de2.php.net/manual/en/function.fsockopen.php

    $errno = 0;
    $errstr = "";
    
    $res = fsockopen('www.example.com', 80, $errno, $errstr);
    
    $request = "GET / HTTP/1.1\r\n";
    $request .= "Host: www.example.com\r\n";
    $request .= "Connection: Close\r\n\r\n";
    
    fwrite($res, $request);
    
    $head = "";
    
    while(!feof($res)) {
        $head .= fgets($res);
    }
    
    $firstLine = reset(explode("\n", $head));
    $matches = array();
    preg_match("/[0-9]{3}/", $firstLine, $matches);
    var_dump($matches[0]);
    

    Curl may be also a good option, but the best option is to beat your admin ;)

提交回复
热议问题