cUrl - 获取html响应正文

ela*_*hen 3 php curl

我相信这很简单.我正在使用下面的函数来检索站点原始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;
}
Run Code Online (Sandbox Code Playgroud)

应将站点原始html分配给$ response,然后将其返回.

lab*_*bue 8

你的代码有效.试试echo htmlentities($response); 你将获得你正在卷曲的网站的原始html.