【问题标题】:get page content through curl call通过 curl 调用获取页面内容
【发布时间】:2020-04-07 15:06:35
【问题描述】:

我正在尝试通过 curl 调用获取页面内容。但我只得到一个空数组。这是我的代码

print_r(get_data('https://www.realestate.com.au/sold/in-9%2f20%3b/list-1'));
function get_data($url)
{
    $ch = curl_init();
    $timeout = 5;
    // the url to fetch
    curl_setopt($ch, CURLOPT_URL, $url);
    // return result as a string rather than direct output
     curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17');
   curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
   curl_setopt($ch, CURLOPT_VERBOSE, 1);
    // set max time of cURL execution

   curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
    }

我也尝试过不同的方法,比如 file_get_contents 函数,但总是得到空白页面。

【问题讨论】:

标签: php curl protected


【解决方案1】:

给定的功能很复杂,我认为它是一个复制粘贴功能,正如我所见 return $result; } 这不是必需的,你应该得到语法错误。

如果没有那个括号,代码可能会工作。

我清除并缩短了代码,并提供了一个可行且(经过测试的)解决方案。

注意:您可以将我删除的代码添加回来

$url 'your url';
//Usaqe of function I recomend to use parse_url();
echo get_data($url);
function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

【讨论】:

  • 您能解释一下您所做的更改以及如何解决问题。
  • 我刚刚尝试过,结果相同。显示空白页creativetech.design/demos/save
  • 通过正常链接,两个代码都可以正常工作。当我们尝试此链接realestate.com.au/sold/in-9%2f20%3b/list-1 时,它不起作用
  • 普通链接是什么意思?你是说 ssl 吗?如果是这样,请将 curl_setopt($ch, CURLOPT_CAINFO, '/path/to/cert/file/cacert.pem'); 添加到您的代码中并在此处下载 cacert 文件 curl.haxx.se/docs/caextract.htmlhttps://curl.haxx.se/docs/… 如果您的意思是带有空格的 url,那么它的 seo_url 问题不是函数。
  • 对不起,我在评论 cacert 文件中添加了两次 url https://curl.haxx.se/docs/caextract.html 你的网站使用这个 url 很好 https://www.realestate.com.au/sold/in-9/list-1 你需要删除 url 中的空格。
猜你喜欢
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-05
  • 2011-09-12
  • 2018-12-23
  • 2010-12-10
相关资源
最近更新 更多