If a request is sent to your server asking to display a webpage on your site (for example, when a user accesses your page through a browser or Googlebot crawls a webpage), the server returns an HTTP status Code response request.
This status code provides information about the status of the request, telling Googlebot about your website and the requested Web page.
Some of the common status codes are:
200– server successfully returned to Web page
404– The requested page does not exist
503– Server Timeout
This article briefly describes the PHP get Web request Status program example as follows:
Method one, using 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); Record Start time list ($usec, $sec) = Explode ("", Microtime (true)); $timer [' start '] = (float) $usec + (float) $sec; Verify the URL if (!preg_match ("/^https?:\/\//i", $url)) {$url = "http://". $url; }//Supports HTTPS if (Preg_match ("/^https:\/\//i", $url)) {$port = 443; }//Parse URL $urlinfo = Parse_url ($url); if (Empty ($urlinfo [' path ')]) {$urlinfo [' path '] = '/'; } $host = $urlinfo [' Host ']; $uri = $urlinfo [' Path ']. (Empty ($urlinfo [' query '])? ': $urlinfo [' query ']); Open connection via 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); }//Submit Request $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 is a simple package of fsockopen function, the efficiency is slightly lower, but the grasp success rate is very high, so in the Snoopy problem when I generally use him. 5.0.0 added support for the context, with the context, he can also send header information, custom user agent, Referer, cookies are not a cinch. 5.1.0 added the offset and MaxLen parameters to read only part of the file.
Method two, using snoopy.class.php
Snoopy is a PHP class that simulates the function of a browser and can retrieve the contents of a Web page and send a form.
$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; 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);