Php code used to obtain the webpage request status

Source: Internet
Author: User
Many webmasters will check whether their website status code is 200 or the error page code is 404. The most common way to view the code is to use webmasters or ff browsers, but a lot of friends don't know how to write a status code by themselves...

Many webmasters will check whether their website status code is 200 or the error page code is 404. The most common way to view the code is to use webmasters or ff browsers, however, many users do not know that they can write a function to view status code.

Method 1: Use fsockopen to seriously despise curl_getinfo. the code is as follows:

  1. Function get_http_code ($ url = "localhost", $ port = 80, $ fsock_timeout = 10 ){
  2. Set_time_limit (0 );
  3. Ignore_user_abort (true );
  4.  
  5. // Record start time
  6. List ($ usec, $ sec) = explode ("", microtime (true ));
  7. $ Timer ['start'] = (float) $ usec + (float) $ sec;
  8.  
  9. // Verify the URL
  10. If (! Preg_match ("/^ https? : \/\ // I ", $ url )){
  11. $ Url = "http: //". $ url;
  12. }
  13. // Supports HTTPS
  14. If (preg_match ("/^ https: \// I", $ url )){
  15. $ Port = 443;
  16. }
  17.  
  18. // Parse the URL
  19. $ Urlinfo = parse_url ($ url );
  20. If (emptyempty ($ urlinfo ['path']) {
  21. $ Urlinfo ['path'] = '/';
  22. }
  23. $ Host = $ urlinfo ['host'];
  24. $ Uri = $ urlinfo ['path']. (emptyempty ($ urlinfo ['query'])? '': $ Urlinfo ['query']);
  25.  
  26. // Open the connection through fsock
  27. If (! $ Fp = fsockopen ($ host, $ port, $ errno, $ error, $ fsock_timeout )){
  28. List ($ usec, $ sec) = explode ("", microtime (true ));
  29. $ Timer ['end'] = (float) $ usec + (float) $ sec;
  30. $ Usetime = (float) $ timer ['end']-(float) $ timer ['start'];
  31.  
  32. Return array ('code' =>-1, 'usetime' => $ usetime );
  33. }
  34.  
  35. // Submit the request
  36. $ Status = socket_get_status ($ fp );
  37. $ Out = "GET {$ uri} HTTP/1.1 \ r \ n ";
  38. $ Out. = "Host: {$ host} \ r \ n ";
  39. $ Out. = "Connection: Close \ r \ n ";
  40. $ Write = fwrite ($ fp, $ out );
  41. If (! $ Write ){
  42. List ($ usec, $ sec) = explode ("", microtime (true ));
  43. $ Timer ['end'] = (float) $ usec + (float) $ sec;
  44. $ Usetime = (float) $ timer ['end']-(float) $ timer ['start'];
  45.  
  46. Return array ('code' =>-2, 'usetime' => $ usetime );
  47. }
  48.  
  49. $ Ret = fgets ($ fp, 1024 );
  50. Preg_match ("/http \/\ d \. \ d \ s (\ d +)/I", $ ret, $ m );
  51. $ Code = $ m [1];
  52. Fclose ($ fp );
  53.  
  54. List ($ usec, $ sec) = explode ("", microtime (true ));
  55. $ Timer ['end'] = (float) $ usec + (float) $ sec;
  56. $ Usetime = (float) $ timer ['end']-(float) $ timer ['start'];
  57.  
  58. Return array ('code' => $ code, 'usetime' => $ usetime );
  59. }

File_get_contents is a simple package of the fsockopen function, which is less efficient, but the capture success rate is very high. so I usually come here when snoopy goes wrong. 5.0.0 adds support for context. with context, it can also send header information, custom user agent, referer, and cookies. 5.1.0 added the offset and maxlen parameters to read only part of the file.

Method 2: Use snoopy. class. php

Snoopy is a php class used to simulate browser functions. it can obtain webpage content and send forms. the code is as follows:

  1. $ Ch = curl_init ();
  2. Curl_setopt ($ ch, CURLOPT_URL, 'http: // www.spiegel.de /');
  3. Curl_setopt ($ ch, CURLOPT_RANGE, '0-500 ');
  4. Curl_setopt ($ ch, CURLOPT_BINARYTRANSFER, 1 );
  5. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
  6. $ Result = curl_exec ($ ch );
  7. Curl_close ($ ch );
  8. Echo $ result;
  9. /**
  10. * But as noted before if the server doesn't honor this header but sends the whole file curl will download all of it. e. g. http://www.111cn.net ignores the header. but you can (in addition) set a write function callback and abort the request when more data is stored ed, e.g.
  11. * Php 5.3 + only
  12. * Use function writefn ($ ch, $ chunk) {...} for earlier versions
  13. */
  14. $ Writefn = function ($ ch, $ chunk ){
  15. Static $ data = '';
  16. Static $ limit = 500; // 500 bytes, it's only a test
  17. $ Len = strlen ($ data) + strlen ($ chunk );
  18. If ($ len >=$ limit ){
  19. $ Data. = substr ($ chunk, 0, $ limit-strlen ($ data ));
  20. Echo strlen ($ data), '', $ data;
  21. Return-1;
  22. }
  23. $ Data. = $ chunk;
  24. Return strlen ($ chunk );
  25. };
  26. $ Ch = curl_init ();
  27. Curl_setopt ($ ch, CURLOPT_URL, 'http: // www.111cn.net /');
  28. Curl_setopt ($ ch, CURLOPT_RANGE, '0-500 ');
  29. Curl_setopt ($ ch, CURLOPT_BINARYTRANSFER, 1 );
  30. Curl_setopt ($ ch, CURLOPT_WRITEFUNCTION, $ writefn );
  31. $ Result = curl_exec ($ ch );
  32. Curl_close ($ ch );

Some common status codes are:

200-the server returns the webpage successfully

404-the requested webpage does not exist

503-server timeout

301-page redirection

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.