php从https获取HTML源代码

时间:2012-04-13 08:45:43

标签: php curl https html-parsing

如何从https获取HTML源代码?我尝试使用curl代替get_file_contests,但仍未获得image src

require dirname(__FILE__) . '/simple_html_dom.php';

$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'https://www.tumblr.com/');
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");
$query = curl_exec($curl_handle);
curl_close($curl_handle);

$html = file_get_html($query);
foreach($html->find('img') as $element) {
   echo $element->src.'<br />';
}

1 个答案:

答案 0 :(得分:1)

更改

$html = file_get_html($query);

为:

$html = str_get_html($query);

file_get_html函数需要一个文件(或URL),而不是变量。