簡體   English   中英

無法通過php curl獲得html dom。

[英]Cant get the html dom by php curl.

我已經使用php CURL來獲取html或回顯html。 但是,當我嘗試使用此代碼時,這是突然重定向。

    $cookie = tempnam ("/tmp", "CURLCOOKIE");  
    $ch = curl_init(); 

  function get_data( $ch, $url, $post, $cookie ){
    $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7"; 
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    //curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); 
    curl_setopt($ch, CURLOPT_HEADER, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    if( $post != '' ) 
      curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    return curl_exec($ch); 
  }
  $url = 'https://iapps.courts.state.ny.us/webcivil/FCASSearch?param=I';
  $html = get_data( $ch, $url, '', '' );
  echo $html; exit;

我玩過這些

CURLOPT_RETURNTRANSFER,
CURLOPT_FOLLOWLOCATION,
CURLOPT_COOKIEJAR,
CURLOPT_COOKIEFILE

但是,當我嘗試獲取html時,仍然得到了重定向。 如何獲取頁面的HTML或還有其他嘗試?

這是獲取頁面代碼的固定工作代碼。

  $cookie = tempnam ("/tmp", "CURLCOOKIE");  
  $ch = curl_init(); 

  function get_data( $curl, $url, $post, $cookie ){
    $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7"; 
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_USERAGENT, $agent);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie); 
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0); 
    curl_setopt($curl, CURLOPT_HEADER, 0); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
    if( $post != '' ) 
      curl_setopt($curl, CURLOPT_POSTFIELDS, $post); 
    return curl_exec($curl); 
  }
  $url = 'https://iapps.courts.state.ny.us/webcivil/FCASSearch?param=I';
  $html = get_data( $ch, $url, '', '' );
  echo htmlspecialchars($html);

但是您看到了什么嗎? 幾乎只有JS解析起來似乎不太有用。

您可以從這段代碼中獲得啟發。 在live_url中提供要從中獲取html內容的頁面的路徑。

$live_url = "http://www.example.com/page/header.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $live_url);
curl_setopt($ch, CURLOPT_TIMEOUT, 1000);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
$res = curl_getinfo($ch);
curl_close($ch);
echo $content;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM