微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – cUrl – 获取html响应正文

我相信这很简单.我正在使用下面的函数来检索网站原始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);您将获得您正在卷曲的网站的原始html.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐