2012-11-13 53 views
0
<?php 
function get_web_page($url) 
{ 
     //echo "curl:url<pre>".$url."</pre><BR>"; 
    $options = array(
     CURLOPT_RETURNTRANSFER => true,  // return web page 
     CURLOPT_HEADER   => false, // don't return headers 
     CURLOPT_FOLLOWLOCATION => true,  // follow redirects 
     CURLOPT_ENCODING  => "",  // handle all encodings 
     CURLOPT_USERAGENT  => "spider", // who am i 
     CURLOPT_AUTOREFERER => true,  // set referer on redirect 
     CURLOPT_CONNECTTIMEOUT => 15,  // timeout on connect 
     CURLOPT_TIMEOUT  => 15,  // timeout on response 
     CURLOPT_MAXREDIRS  => 10,  // stop after 10 redirects 

    ); 

    $ch  = curl_init($url); 
    curl_setopt_array($ch, $options); 
    $content = curl_exec($ch); 
    $err  = curl_errno($ch); 
    $errmsg = curl_error($ch); 
    $header = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL); 
    curl_close($ch); 

    $header['errno'] = $err; 
    $header['errmsg'] = $errmsg; 

    //change errmsg here to errno 
    if ($errmsg) 
    { 
     echo "CURL:".$errmsg."<BR>"; 
    } 
    return $content; 
} 
print_r(get_web_page('http://google.com')); 
?> 

爲什麼在這個例子中我沒有圖像和CSS?我怎麼才能得到它?簡單地解決這個問題?我必須在所有鏈接之前添加http://google.com,但如何?捲曲 - 獲取頁面的圖像和css

+0

你知道圖像和css不會自動成爲網頁的一部分嗎?瀏覽器(例如)必須單獨關注所有鏈接,並逐一請求每個資源(通常是並行)從其各自的URL下載鏈接的內容。理解這一點的一個簡單方法是在後臺運行像Fiddler這樣的代理時訪問您的測試頁面,並觀察通過網絡傳遞的請求。 – mellamokb

回答

2

你是在一個庫中可能有興趣像這樣:http://phpcrawl.cuab.de/

的libcurl本身不能充當的WebCrawler,因爲它不分析網頁內容。這是你必須自己做的工作。