php擷取網頁請求狀態程式碼

來源:互聯網
上載者:User

方法一,使用 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(empty($urlinfo['path'])){
        $urlinfo['path'] = '/';
    }
    $host = $urlinfo['host'];
    $uri = $urlinfo['path'] . (empty($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 - 頁面重新導向

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.