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;
}