php Get Web Page Request Status Program Example

  • 2021-06-29 10:35:17
  • OfStack

For web page return status code 1, we usually check whether our website status code is 200 or the error page is 404, and in most cases our view method is to use the webmaster tool or ff browser to view, very few people think of the function of writing a view status code by themselves.

This article outlines an example of php's Web page request status program as follows:

Method 1, using fsockopen
(curl_is not recommendedgetinfo!)


function get_http_code($url="localhost", $port=80, $fsock_timeout=10){
    set_time_limit(0);
    ignore_user_abort(true);

    //  Record start time 
    list($usec, $sec) = explode(" ", microtime(true));
    $timer['start'] = (float)$usec + (float)$sec;

    //  check URL
    if(!preg_match("/^https?: Bat / Bat //i", $url)){
        $url = "http://".$url;
    }
    //  Support HTTPS
    if(preg_match("/^https: Bat / Bat //i", $url)){
        $port = 443;
    }

    //  analysis URL
    $urlinfo = parse_url($url);
    if(empty($urlinfo['path'])){
        $urlinfo['path'] = '/';
    }
    $host = $urlinfo['host'];
    $uri = $urlinfo['path'] . (empty($urlinfo['query'])?'':$urlinfo['query']);

    //  adopt fsock open a connection 
    if(!$fp = fsockopen($host, $port, $errno, $error, $fsock_timeout)){
        list($usec, $sec) = explode(" ", microtime(true));
        $timer['end'] = (float)$usec + (float)$sec;
        $usetime = (float)$timer['end'] - (float)$timer['start'];

        return array('code'=>-1, 'usetime'=>$usetime);
    }

    //  Submit Request 
    $status = socket_get_status($fp);
    $out = "GET {$uri} HTTP/1.1 Bat r Bat n";
    $out .= "Host: {$host} Bat r Bat n";
    $out .= "Connection: Close Bat r Bat n Bat r Bat n";
    $write = fwrite($fp, $out);
    if(!$write){
        list($usec, $sec) = explode(" ", microtime(true));
        $timer['end'] = (float)$usec + (float)$sec;
        $usetime = (float)$timer['end'] - (float)$timer['start'];

        return array('code'=>-2, 'usetime'=>$usetime);
    }

    $ret = fgets($fp, 1024);
    preg_match("/http Bat / Bat d Bat . Bat d Bat s( Bat d+)/i", $ret, $m);
    $code = $m[1];
    fclose($fp);

    list($usec, $sec) = explode(" ", microtime(true));
    $timer['end'] = (float)$usec + (float)$sec;
    $usetime = (float)$timer['end'] - (float)$timer['start'];

    return array('code'=>$code, 'usetime'=>$usetime);
}

file_get_contents is a simple package of fsockopen features, which is slightly less efficient, but has a high capture success rate, so I usually use it when snoopy has problems.5.0.0 adds support for context, with context, he can also send header information, custom users agent, referer, cookies are not in the conversation.5.1.0 adds the offset and maxlen parameters, which allow you to read only a portion of the file.

Method 2, using snoopy.class.php

Snoopy is an php class that mimics the capabilities of browsers to get web content and send forms.


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.ofstack.com/');
curl_setopt($ch, CURLOPT_RANGE, '0-500');
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
$writefn = function($ch, $chunk) {
  static $data='';
  static $limit = 500; // 500 bytes, it's only a test
  $len = strlen($data) + strlen($chunk);
  if ($len >= $limit ) {
    $data .= substr($chunk, 0, $limit-strlen($data));
    echo strlen($data) , ' ', $data;
    return -1;
  }
  $data .= $chunk;
  return strlen($chunk);
};
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.ofstack.com/');
curl_setopt($ch, CURLOPT_RANGE, '0-500');
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, $writefn);
$result = curl_exec($ch);
curl_close($ch);

1 Some common status codes are:
200 - Server successfully returned to web page
404 - The requested page does not exist
503 - Server Timeout
301 - Page redirection


Related articles: