我相信这是相当简单的。我使用下面的函数来检索网站的原始html为了解析它。在我的测试中,我决定在stackoverflow.com上运行我的代码
而不是得到html响应的Chrome打印出实际的网站,而不是将html分配给它的真实性。我错过了什么?
function get_site_html($site_url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 4);
curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_URL, $site_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
global $base_url;
$base_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$http_response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
return $response;
}
网站的原始html应该分配给$response,然后返回它。
你的代码工作了。试试echo htmlentities($response);
,你会得到原始的html为您的网站卷曲