我相信这相当简单。我正在使用下面的函数来检索网站的原始 html 为了解析它。在测试期间,我决定在 stackoverflow.com 上运行我的代码
Chrome 不是获取 html 响应,而是打印出实际站点,而不是将 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;
}
The site raw html should be assigned to $response, and then return it.
最佳答案
您的代码有效。尝试 echo htmlentities($response);
您将获得您正在 curl 的网站的原始 html。
关于php - cUrl - 获取 html 响应正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17134546/