在php中,下載http資源比較常用的一種做法就是用 file_get_contents 這個函數,但這函數無法獲得http頭的信息,會給一些應用帶來不方便,因此我寫了一個http下載類來解決這個問題,測試感覺速度和file_get_contents相差無幾。
<?
class DedeHttpDown
{
var $m_url = "";
var $m_urlpath = "";
var $m_scheme = "http";
var $m_host = "";
var $m_port = "80";
var $m_user = "";
var $m_pass = "";
var $m_path = "/";
var $m_query = "";
var $m_fp = "";
var $m_error = "";
var $m_httphead = "";
var $m_html = "";
//
//初始化系統
//
function PrivateInit($url)
{
$urls = "";
$urls = @parse_url($url);
$this->m_url = $url;
if(is_array($urls))
{
$this->m_host = $urls["host"];
if(!empty($urls["scheme"])) $this->m_scheme = $urls["scheme"];
if(!empty($urls["user"])){
$this->m_user = $urls["user"];
}
if(!empty($urls["pass"])){
$this->m_pass = $urls["pass"];
}
if(!empty($urls["port"])){
$this->m_port = $urls["port"];
}
if(!empty($urls["path"])) $this->m_path = $urls["path"];
$this->m_urlpath = $this->m_path;
if(!empty($urls["query"]))
{
$this->m_query = $urls["query"];
$this->m_urlpath .= "?".$this->m_query;
}
}
}
//
//打開指定網址
//
function OpenUrl($url)
{
//重設各參數
$this->m_url = "";
$this->m_urlpath = "";
$this->m_scheme = "http";
$this->m_host = "";