解析网页而不保存本地?

时间:2013-09-01 04:25:53

标签: php curl save

对不起我的英语。我正在使用CURL,因为网页需要此功能。我没有获得页面的 file_get_contents 。如何在没有页面保存的情况下解析页面? (FOPEN,fwrite的)

<?PHP
function fileGet($url, $timeout = 55, $ref=true){
    $useri = $_SERVER['HTTP_USER_AGENT'];
    @set_time_limit($timeout);  
    $curl = curl_init();        
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_COOKIESESSION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION,true);
    curl_setopt($curl, CURLOPT_COOKIE, 'PHPSESSID=fztitsfgsafafaq25llwafd0; path:/' );
    curl_setopt($curl, CURLOPT_USERAGENT, $useri);
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); 
    curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($curl, CURLOPT_REFERER,$url);
    $data = curl_exec($curl);
    curl_close($curl);
    // Save Page Start
    $fp = fopen('data.html', 'w');
    fwrite($fp, $data); 
    fclose($fp);
    // Save Page End
    return $data;
}
    // Start Code
fileGet("http://www.example.com",10); // Start Function
$html = file_get_html('data.html'); // Open Saved Page In Local
    foreach($html->find('div.columns') as $article) {
        // Events.....
        mysql_query("Insert Query");
    }
    // End Code
?>

1 个答案:

答案 0 :(得分:0)

<?PHP
function fileGet($url, $timeout = 55, $ref=true){
    $useri = $_SERVER['HTTP_USER_AGENT'];
    @set_time_limit($timeout);  
    $curl = curl_init();        
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_COOKIESESSION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION,true);
    curl_setopt($curl, CURLOPT_COOKIE, 'PHPSESSID=fztitsfgsafafaq25llwafd0; path:/' );
    curl_setopt($curl, CURLOPT_USERAGENT, $useri);
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); 
    curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($curl, CURLOPT_REFERER,$url);
    $data = curl_exec($curl);
    return $data;
}
    // Start Code
$html = str_get_html(fileGet("http://www.example.com",10));
    foreach($html->find('div.columns') as $article) {
        // Events.....
        mysql_query("Insert Query");
    }
    // End Code
?>