张大佬张大佬

PHP获取访问页面HTTP状态码的实现代码

<!DOCTYPE HTML> 

<html>

<head>

<meta charset="utf-8">

<title>菜鸟教程(runoob.com)</title>

</head>

<body> 

a=

<?php

$ch = curl_init ( 'http://1.1.1.1/' );

curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );

curl_exec ( $ch );

$httpcode = curl_getinfo ( $ch, CURLINFO_HTTP_CODE ); // 200

curl_close ( $ch );

$a=$httpcode;

echo $a;

?>

<br/>

b=

<?php

$ch = curl_init ( 'http://1.1.1.1/' );

curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );

curl_exec ( $ch );

$httpcode = curl_getinfo ( $ch, CURLINFO_HTTP_CODE ); // 200

curl_close ( $ch );

$b=$httpcode;

echo $b;

?>

<br/>

c=

<?php

$ch = curl_init ( 'http://1.1.1.1/' );

curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );

curl_exec ( $ch );

$httpcode = curl_getinfo ( $ch, CURLINFO_HTTP_CODE ); // 200

curl_close ( $ch );

$c=$httpcode;

echo $c;

?>

</body>

</html>








方法一


$header_info=get_headers('//www.jb51.net/');

echo $header_info[0];

//耗时 0.67~0.78

方法二

$ch = curl_init ( '//www.jb51.net/' );

curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );

curl_exec ( $ch );

$httpcode = curl_getinfo ( $ch, CURLINFO_HTTP_CODE ); // 200

curl_close ( $ch );

echo $httpcode;

//耗时 0.84~0.94

方法三给curl设置一些参数,可以加快速度,代码来自网络

$curl = curl_init();

$url='//www.jb51.net/';

curl_setopt($curl, CURLOPT_URL, $url); //设置URL

curl_setopt($curl, CURLOPT_HEADER, 1); //获取Header

curl_setopt($curl,CURLOPT_NOBODY,true); //Body就不要了吧,我们只是需要Head

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //数据存到成字符串吧,别给我直接输出到屏幕了

curl_exec($curl); //开始执行啦~

$httpcode=curl_getinfo($curl,CURLINFO_HTTP_CODE); //我知道HTTPSTAT码哦~

curl_close($curl); //用完记得关掉他

echo $httpcode;

//耗时 0.45~0.53

代码四


核心代码:

/**

 * 获取远程URL的HTTP状态

 *

 * @version 0.0.1

 * @Author Chenjl *

 * @param string $url  远程URL

 * @param string $data  ture[返回HTTP状态数组] | false[返回状态数值]

 *

 * @return mixed

 */

function getHeaders($url,$data=FALSE){

 $_headers = get_headers($url,1);

 if( !$data ){return $_headers;}

 $curl = curl_init();

 curl_setopt($curl,CURLOPT_URL,$url);//获取内容url

 curl_setopt($curl,CURLOPT_HEADER,1);//获取http头信息

 curl_setopt($curl,CURLOPT_NOBODY,1);//不返回html的body信息

 curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);//返回数据流,不直接输出

 curl_setopt($curl,CURLOPT_TIMEOUT,30); //超时时长,单位秒

 curl_exec($curl);

 $rtn= curl_getinfo($curl,CURLINFO_HTTP_CODE);

 curl_close($curl);

 return $rtn;

}

上面的代码,逐风已将两种获取方式进行一个整合,方便不同获取HTTP状态情景的需求;


返回结果:


# 调用案例1:getHeaders('//www.jb51.net\/\',true);

# 返回结果:200 // 直接返回HTTP状态码

   

# 调用案例2:getHeaders('//www.jb51.net/',false);

# 返回结果:

/*

array(10) {

[0]=>

string(15) "HTTP/1.1 200 OK"

["Server"]=>

string(5) "nginx"

["Date"]=>

string(29) "Mon, 04 Jul 2016 06:21:35 GMT"

["Content-Type"]=>

string(9) "text/html"

["Content-Length"]=>

string(5) "26898"

["Last-Modified"]=>

string(29) "Mon, 04 Jul 2016 06:16:00 GMT"

["Connection"]=>

string(5) "close"

["Vary"]=>

string(15) "Accept-Encoding"

["ETag"]=>

string(15) ""5779ff20-6912""

["Accept-Ranges"]=>

string(5) "bytes"

}

*/


本原创文章未经允许不得转载 | 当前页面:张大佬 » PHP获取访问页面HTTP状态码的实现代码

评论