【问题标题】:cUrl - getting the html response bodycUrl - 获取 html 响应正文
【发布时间】:2013-06-16 14:55:02
【问题描述】:

我相信这相当简单。我正在使用下面的函数来检索网站原始 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;
}

网站原始html应该分配给$response,然后返回。

【问题讨论】:

  • 请在堆栈溢出时使用空格进行适当的缩进。
  • 您发布的内容看起来不错。您确定代码中的其他地方没有问题吗?

标签: php curl


【解决方案1】:

您的代码有效。试试echo htmlentities($response); 你会得到你正在卷曲的网站的原始html。

【讨论】:

    猜你喜欢
    • 2019-09-30
    • 2015-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2015-04-06
    • 1970-01-01
    • 2015-12-05
    相关资源
    最近更新 更多