首页  >  文章  >  后端开发  >  尽管出现“页面已移动”错误,如何使用 cURL 检索页面内容?

尽管出现“页面已移动”错误,如何使用 cURL 检索页面内容?

Patricia Arquette
Patricia Arquette原创
2024-10-22 20:52:03437浏览

How to Retrieve Page Content Using cURL Despite

使用 cURL 检索页面内容

在此上下文中,您试图使用 cURL 抓取 Google 搜索结果页面的内容。尽管尝试设置用户代理和各种选项,但您仍无法成功检索页面内容。重定向或“页面移动”错误继续困扰着您。

据信该问题可能源于查询字符串中特殊字符的编码。为了缓解这种情况,需要更改 PHP 代码。

方法如下:

<code class="php">function get_web_page($url)
{
    $user_agent = 'Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';

    $options = array(
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_POST           => false,
        CURLOPT_USERAGENT      => $user_agent,
        CURLOPT_COOKIEFILE     => "cookie.txt",
        CURLOPT_COOKIEJAR      => "cookie.txt",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HEADER         => false,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_ENCODING       => "",
        CURLOPT_AUTOREFERER    => true,
        CURLOPT_CONNECTTIMEOUT => 120,
        CURLOPT_TIMEOUT        => 120,
        CURLOPT_MAXREDIRS      => 10
    );

    $ch = curl_init($url);
    curl_setopt_array($ch, $options);
    $content = curl_exec($ch);
    $err = curl_errno($ch);
    $errmsg = curl_error($ch);
    $header = curl_getinfo($ch);
    curl_close($ch);

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}</code>

用法:

<code class="php">$result = get_web_page($url);

if ($result['errno'] != 0) {
    // Handle errors: bad URL, timeout, redirect loop
}

if ($result['http_code'] != 200) {
    // Handle errors: no page, no permissions, no service
}

$page = $result['content'];</code>

使用此方法代码后,您现在可以检索浏览器中显示的确切页面内容。通过考虑查询字符串中的特殊字符,您可以克服之前遇到的障碍。

以上是尽管出现“页面已移动”错误,如何使用 cURL 检索页面内容?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn