Home  >  Article  >  Backend Development  >  Example of php getting web page request status code

Example of php getting web page request status code

怪我咯
怪我咯Original
2017-07-14 15:04:183034browse

If a request is sent to your server to display a page on your site (for example, when a user accesses your page through a browser or when Googlebot crawls the page), the server will return HTTP Status codeResponse to the request.

This status code provides information about the status of the request, telling Googlebot about your site and the requested page.

Some common status codes are:

200 – The server successfully returned the web page

404 – The requested web page does not exist

503 – Server timed out

This article briefly describes the program example of php to obtain the web page request status as follows:

Method 1, use fsockopen

The code is as follows:

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

    // 记录开始时间
    list($usec, $sec) = explode(" ", microtime(true));
    $timer['start'] = (float)$usec + (float)$sec;

    // 校验URL
    if(!preg_match("/^https?:\/\//i", $url)){
        $url = "http://".$url;
    }
    // 支持HTTPS
    if(preg_match("/^https:\/\//i", $url)){
        $port = 443;
    }

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

    // 通过fsock打开连接
    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);
    }

    // 提交请求
    $status = socket_get_status($fp);
    $out = "GET {$uri} HTTP/1.1\r\n";
    $out .= "Host: {$host}\r\n";
    $out .= "Connection: Close\r\n\r\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\/\d\.\d\s(\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 It is a simple package of the fsockopen function. It is slightly less efficient, but the capture success rate is very high, so I usually use it when snoopy has problems. 5.0.0 adds support for context. With context, it can also send header information, and customize user agents, referers, and cookies. 5.1.0 added offset and maxlen parameters to read only part of the file.

Method 2, use snoopy.class.php

Snoopy is a php class that is used to simulate the functions of a browser and can obtain web content and send forms.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.jb51.net/');
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, 'http://www.jb51.net/');
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);

The above is the detailed content of Example of php getting web page request status code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn