不能通过php curl获取html dom。

时间:2016-10-17 06:05:57

标签: 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或者还有其他的尝试吗?

2 个答案:

答案 0 :(得分:0)

这是一个固定的工作代码,用于获取页面代码。

  $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 这似乎不是非常有用的解析。

答案 1 :(得分:0)

您可以从此代码中了解。在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;