Php retrieves http header request status information

Source: Internet
Author: User
When we use the webmaster tool, we will find that there is a website http status information. In fact, this function is very simple to use php. we can use curl to implement the following examples. to use curl, you need. enable settings in ini... when we use the webmaster tool, we will find that there is a website http status information. In fact, this function is very simple to use php. we can use curl to implement the following examples.


To use curl, you must enable it in php. ini> <on a Windows Server, open php. ini and find:


Extension = php_curl.dll.


The implementation code is as follows:

$ Curl = curl_init (); $ url = 'http: // www.phprm.com '; curl_setopt ($ curl, CURLOPT_URL, $ url); // Set URL curl_setopt ($ curl, CURLOPT_HEADER, 1); // Get the Header curl_setopt ($ curl, CURLOPT_NOBODY, true); // The Body will not be needed. we just need Head curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 ); // save the data to a string. don't output it to the screen directly. $ data = curl_exec ($ curl); // start execution ~ Echo curl_getinfo ($ curl, CURLINFO_HTTP_CODE); // I know the HTTPSTAT code ~ Curl_close ($ curl); // when used up, remember to turn it off.

Alternatively, use the get_headers function to get all the headers sent by the server in response to an HTTP request. The example code is as follows:


In php, if we want to obtain data, simulate login, POST data, and other functions, we must first think of the curl function, this function is convenient and practical and supports multithreading. the following example is provided. if you are interested, refer to it.

Example: use php curl to obtain webpage data. the code is as follows:

 "Syxrrrr", "pw" => "123456"); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data); $ output = curl_exec ($ ch); curl_close ($ ch ); echo $ output;

The code is as follows:

/********************** Curl series *************** * ******* // Obtain data (including POST and HEADER) through curl) /** $ url: if it is not an array, it is http. if it is an array, it is https * $ header: header file * $ post: submit the array form in post mode * $ cookies: 0: No cookie by default; 1: Set; 2: Get */public function curl_allinfo ($ urls, $ header = FALSE, $ post = FALSE, $ cookies = 0) {$ url = is_array ($ urls )? $ Urls ['0']: $ urls; $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); // submit if ($ header! = FALSE) {curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header);} // if ($ post! = FALSE) {curl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post);} if ($ cookies = 1) {curl_setopt ($ ch, CURLOPT_COOKIEJAR, "cookiefile");} else if ($ cookies = 2) {curl_setopt ($ ch, CURLOPT_COOKIEFILE, "cookiefile");} if (is_array ($ urls )) {curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false);} $ data = curl_exec ($ ch); curl_close ($ ch ); return $ data ;}

Finally, a web page is crawled by imitating a search engine spider. the code is as follows:

Function get_web_page ($ url) {$ options = array (CURLOPT_RETURNTRANSFER => true, // return the web page CURLOPT_HEADER => false, // do not return the returned information CURLOPT_FOLLOWLOCATION => true, // follow redirects CURLOPT_ENCODING => "", // handle all encodings CURLOPT_USERAGENT => "spider", // Set UserAgent CURLOPT_AUTOREFERER => true, // set referer on redirect CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect connection timeout CURLOPT_TIMEOUT => 120, // timeout on response reply timeout CURLOPT_MAXREDIRS => 10, // stop after 10 redirects); $ ch = curl_init ($ url); curl_setopt_array ($ ch, $ options); $ content = curl_exec ($ ch ); $ err = curl_errno ($ ch); $ errmsg = curl_error ($ ch); $ header = curl_getinfo ($ ch); curl_close ($ ch ); $ header [''errno''] = $ err; $ header [''errmsg ''] = $ errmsg; $ header [''content''] = $ content; return $ header ;}


Link to this article:

Add to favorites ^ please keep the tutorial address.

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.