PHP obtains the header information of a remote URL.

Source: Internet
Author: User
Tags ranges

1. Use PHP to obtain the header information of the remote URL. This method is useful when collecting the header information. It allows you to determine whether the remote file or webpage is normal and whether it is 404 pages.

$ Url = 'HTTP: // www.example.com ';

Print_r (get_headers ($ URL ));

The output in the above example is similar:

Array
(
[0] => HTTP/1.1 200 OK
[1] => date: sat, 29 May 2004 12:28:13 GMT
[2] => server: Apache/1.3.27 (UNIX) (red-hat/Linux)
[3] => last-modified: Wed, 08 Jan 2003 23:11:55 GMT
[4] => etag: "3f80f-1b6-3e1cb03b"
[5] => Accept-ranges: bytes
[6] => Content-Length: 438
[7] => connection: Close
[8] => Content-Type: text/html
)

---------------------------------------------------

$ Url = 'HTTP: // www.example.com ';

Print_r (get_headers ($ URL, 1 ));

Array
(
[0] => HTTP/1.1 200 OK
[Date] => sat, 29 May 2004 12:28:14 GMT
[Server] => Apache/1.3.27 (UNIX) (red-hat/Linux)
[Last-modified] => wed, 08 Jan 2003 23:11:55 GMT
[Etag] => "3f80f-1b6-3e1cb03b"
[Accept-ranges] => bytes
[Content-Length] = & gt; 438
[Connection] => close
[Content-Type] => text/html
)

Get_headers
Is used to obtain the response header information of the remote server. You can use the first returned array and the regular expression to determine whether the remote address is 200 normal webpage.

--------------------------------------------------------------

2. Use curl
The curlopt_nobody parameter only captures header information.

The curl function is really a good thing. The curl parameter can be configured to capture only the header information of the remote webpage.

The following code specifies that the content captured by curl contains the header and does not contain the body content.

Function get_header ($ URL ){
$ CH = curl_init ();
Curl_setopt ($ ch, curlopt_url, $ URL );
Curl_setopt ($ ch, curlopt_header, true );
Curl_setopt ($ ch, curlopt_nobody, true );
Curl_setopt ($ ch, curlopt_returntransfer, true );
Curl_setopt ($ ch, curlopt_followlocation, true );
Curl_setopt ($ ch, curlopt_autoreferer, true );
Curl_setopt ($ ch, curlopt_timeout, 30 );
Curl_setopt ($ ch, curlopt_httpheader, array (
'Accept :*/*',
'User-AGENT: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; sv1 )',
'Connection: Keep-alive '));
$ Header = curl_exec ($ ch );
Return $ header;
}


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.