php - cUrl - 获取 html 响应正文

标签 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;
}

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/

相关文章:

PHPExcel模式样式

php - 联系表格 7 - 动态收件人电子邮件

php - 使用 PDO 不会插入任何内容

json - 解析来自curl POST 的HTTP 响应

php - 带有 CURL 的 PHP 中的 SOAP 请求

ruby - 向 Slack 端点 Ruby 发送请求时出现问题

c - 将 CURL 结果放入字符串而不是 STDOUT?

php - 通过 PHP 解码数字 html 实体

php - 隐藏的默认表单值

php - YQL : Getting unsupported http protocol error