網頁返回狀態代碼很多站長會去查自己網站狀態碼是不是200或錯誤頁面是不是404代碼了,那么我們使用最多的查看方法就是使用站長工具或ff瀏覽器來查,但有很多朋友不知道可以自己寫一個查看狀態代碼的功能.
方法一,使用 fsockopen,嚴重鄙視curl_getinfo,代碼如下:
- function get_http_code($url="localhost", $port=80, $fsock_timeout=10){
- set_time_limit(0);
- ignore_user_abort(true);
- // 記錄開始時間
- list($usec, $sec) = explode(" ", microtime(true));
- $timer['start'] = (float)$usec + (float)$sec;
- // 校驗URL
- if(!preg_match("/^https?:\/\//i", $url)){
- $url = "http://".$url;
- }
- // 支持HTTPS
- if(preg_match("/^https:\/\//i", $url)){
- $port = 443;
- }
- // 解析URL
- $urlinfo = parse_url($url);
- if(emptyempty($urlinfo['path'])){
- $urlinfo['path'] = '/';
- }
- $host = $urlinfo['host'];
- $uri = $urlinfo['path'] . (emptyempty($urlinfo['query'])?'':$urlinfo['query']);
- // 通過fsock打開連接
- if(!$fp = fsockopen($host, $port, $errno, $error, $fsock_timeout)){
- list($usec, $sec) = explode(" ", microtime(true));
- $timer['end'] = (float)$usec + (float)$sec;
- $usetime = (float)$timer['end'] - (float)$timer['start'];
- return array('code'=>-1, 'usetime'=>$usetime);
- }
- // 提交請求
- $status = socket_get_status($fp);
- $out = "GET {$uri} HTTP/1.1\r\n";
- $out .= "Host: {$host}\r\n";
- $out .= "Connection: Close\r\n\r\n";
- $write = fwrite($fp, $out);
- if(!$write){
- list($usec, $sec) = explode(" ", microtime(true));
- $timer['end'] = (float)$usec + (float)$sec;
- $usetime = (float)$timer['end'] - (float)$timer['start'];
- return array('code'=>-2, 'usetime'=>$usetime);
- }
- $ret = fgets($fp, 1024);
- preg_match("/http\/\d\.\d\s(\d+)/i", $ret, $m);
- $code = $m[1];
- fclose($fp);
- list($usec, $sec) = explode(" ", microtime(true));
- $timer['end'] = (float)$usec + (float)$sec;
- $usetime = (float)$timer['end'] - (float)$timer['start'];
- return array('code'=>$code, 'usetime'=>$usetime);
- }
file_get_contents 是 fsockopen 功能的簡單打包,效率稍低些,但是抓取成功率很高,所以在 snoopy 出問題的時候我一般那他來。5.0.0 添加了對 context 的支持,有了context,他也可以發送 header 信息,自定義用戶 agent, referer, cookies 都不在話下。5.1.0 添加了 offset 和 maxlen 參數,可以只讀文件的一部分內容。
方法二,使用snoopy.class.php
Snoopy是一個php類,用來模擬瀏覽器的功能,可以獲取網頁內容,發送表單,代碼如下:
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, 'http://www.spiegel.de/');
- curl_setopt($ch, CURLOPT_RANGE, '0-500');
- curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $result = curl_exec($ch);
- curl_close($ch);
- echo $result;
- /**
- *But as noted before if the server doesn't honor this header but sends the whole file curl will download all of it. E.g. http://www.111cn.net ignores the header. But you can (in addition) set a write function callback and abort the request when more data is received, e.g.
- * php 5.3+ only
- * use function writefn($ch, $chunk) { ... } for earlier versions
- */
- $writefn = function($ch, $chunk) {
- static $data='';
- static $limit = 500; // 500 bytes, it's only a test
- $len = strlen($data) + strlen($chunk);
- if ($len >= $limit ) {
- $data .= substr($chunk, 0, $limit-strlen($data));
- echo strlen($data) , ' ', $data;
- return -1;
- }
- $data .= $chunk;
- return strlen($chunk);
- };
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, 'http://www.111cn.net/');
- curl_setopt($ch, CURLOPT_RANGE, '0-500');
- curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
- curl_setopt($ch, CURLOPT_WRITEFUNCTION, $writefn);
- $result = curl_exec($ch);
- curl_close($ch);
一些常見的狀態碼為:
200 - 服務器成功返回網頁
404 - 請求的網頁不存在
503 - 服務器超時
301 - 頁面重定向
新聞熱點
疑難解答