PHP gets HTTP header Request status information

Source: Internet
Author: User

To use curl, you need to set up php.ini >< windows in the server, open php.ini, and find:

Extension=php_curl.dll

Removing the previous comment is both available.

The implementation code is as follows:

The code is as follows Copy Code

$curl = Curl_init ();
$url = ' http://www.111cn.net ';
curl_setopt ($curl, Curlopt_url, $url); Set URL
curl_setopt ($curl, Curlopt_header, 1); Get header
curl_setopt ($curl, curlopt_nobody,true); No, we just need head.
curl_setopt ($curl, Curlopt_returntransfer, 1); Save the data to a string, don't output it directly to the screen.
$data = curl_exec ($curl); Start Execution ~
Echo Curl_getinfo ($curl, Curlinfo_http_code); I know Httpstat code OH ~
Curl_close ($curl); When you finish, turn him off.

or use the Get_headers function directly

Get_headers-gets the server to respond to all headers sent by an HTTP request

Example

  code is as follows copy code

<?php
If (!function_exists (' get_headers ')) {
Function get_headers ($url, $format =0) {
$url =parse_url ($url);
$end = "\r\n\r\n";
$fp =fsockopen ($url [' Host '], (Empty ($url [' Port ']): $url [' Port ']), $errno, $errstr, 30);
if ($fp) {
$out = "get/http/1.1\r\n";
$out. = "Host:". $url [' Host ']. " \ r \ n ";
$out. = "connection:close\r\n\r\n";
$var = ';
Fwrite ($fp, $out);
while (!feof ($fp)) {
$var. =fgets ($FP, 1280);
if (Strpos ($var, $end)
break;
}
Fclose ($FP);
$var =preg_replace ("/\r\n\r\n.*\$/", "", $var);
$var =explode ("\ r \ n", $var);
if ($format) {
foreach ($var as $i) {
if (Preg_match/^ ([a-za-z-]+): + (. *) $/', $i, $parts)
$v [$parts [1]]= $parts [2];
}
return $v;
} else{
    return $var;
   
}
}
}
Echo ' <pre> ';
Print_r (get_headers (' http://www.111cn.net '));

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.