PHP get Web Request Status Program Sample _php tutorial

Source: Internet
Author: User
For Web page return status code in general we will go to check their own site status code is not 200 or error page is not 404 code, and in most cases our view method is to use webmaster tools or FF browser, etc. to view, very few people think of themselves to write a View status code function.

This article briefly describes the PHP get Web request Status program example as follows:

method One, using Fsockopen
(not recommended for use with curl_getinfo!)
Copy CodeThe 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;

Verifying URLs
if (!preg_match ("/^https?:\/\//i", $url)) {
$url = "http://". $url;
}
Support HTTPS
if (Preg_match ("/^https:\/\//i", $url)) {
$port = 443;
}

Parsing URLs
$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.

Copy the Code code as follows:
$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);

Some of the common status codes are:
200-Server successfully returned to Web page
404-The requested page does not exist
503-Server Timeout
301-page Redirection

http://www.bkjia.com/PHPjc/825353.html www.bkjia.com true http://www.bkjia.com/PHPjc/825353.html techarticle for Web page return status code in general we will go to check their own site status code is not 200 or error page is not 404 code, and most of the way our view is to make ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.