cUrl-获取html响应正文 [英] cUrl - getting the html response body

查看:161
本文介绍了cUrl-获取html响应正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我敢肯定这很简单。我正在使用下面的函数来检索网站原始html
进行解析。在测试过程中,我决定在stackoverflow.com上运行代码。

I'm sure this is fairly simple. I'm using the function below to retrieve sites raw html in order to parse it. during my testing, I decided to run my code on stackoverflow.com

Chrome并没有得到html响应,而是打印出了实际的网站,而是将html分配给了它的实际位置。名副其实的。我缺少什么?

Instead of getting the html response the Chrome is printing out the actual site rather then assigning the html to its veritable. What am I missing?

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应该分配给$响应,然后将其返回。

The site raw html should be assigned to $response, and then return it.


推荐答案

您的代码有效。尝试 echo htmlentities($ response); 您将获得要卷曲的网站的原始html。

Your code works. Try echo htmlentities($response); You'll get the raw html for the site you're curling.

这篇关于cUrl-获取html响应正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆