在重定向标题302之后,cURL网络抓取导致404

时间:2018-01-20 01:23:06

标签: php curl web-scraping

我正在这个网络抓取领域开始,不幸的是在主题中没有很多资源要记录,所以我遇到了这个问题。

首先,我试图在一个有足够重定向的网络平台上登录,我会尝试解释我注意到的步骤。

这种方式是我在登录表单中发送数据的方式:

$headers = array(
    'Referer' => 'url from past step',
    'Content-Type'=>'application/x-www-form-urlencoded'
);
$respuesta = $this->getPostResponse(
    'this is the first url',
    array(
        'option'=>'credential',
        'target'=>'this is the second url (don't know why is here)',
        'user_id'=>$user,
        'password'=>$contrasena,
        'submit'=>'Enviar'
     ),
     $headers
);

在此之后,我收到以下答案:

接头:

HTTP/1.1 200 OK 
Server: Apache-Coyote/1.1 
Cache-Control: no-cache 
Content-Type: text/html;charset=ISO-8859-1 
Content-Length: 319

内容:

<html lang="en">
<head>
    <META HTTP-EQUIV="expires" CONTENT="0">
</head>
<body>
    <script language="JavaScript">

        location.href='url that still works';

    </script>
</body>
</html>

所以在这里我做了location.href

$headers = array(
     'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
     'Referer' => 'the url from start',
     'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36'
);

$respuesta = $this->getGetResponse('url that still works',$headers);

并收到以下回复:

接头:

HTTP/1.1 302 Found
Location: THE PROBLEM STARTS WITH THIS URL
Content-Length: 0
Date: Sat, 20 Jan 2018 01:06:05 GMT

答案是我的预期,但事实上这是一个重定向页面是我无法理解的事实,我尝试使用cURL没有重定向(CURLOPT_FOLLOWLOCATION =&gt; false)所以我手动制作获取请求'到那个链接并且没有用,它也没有用相反的方法(CURLOPT_FOLLOWLOCATION =&gt; true)

无论我是否使cURL重定向或如果我'手动',我都会从该位置获得此响应:

接头

HTTP/1.1 404 Not Found

什么时候应该

HTTP/1.1 302 Found
Location: ANOTHER REDIRECT TO FINAL STEP

也许标题是问题?或者这是我面临重定向的方式?有人可以帮我找到这个吗?

这是我的cURL配置:

public static $defaultOptions = array(
    CURLOPT_ENCODING       => "UTF-8",
    CURLOPT_USERAGENT      => "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER         => true,
    CURLOPT_FOLLOWLOCATION => false,
    CURLOPT_AUTOREFERER    => true,
    CURLOPT_CONNECTTIMEOUT => 120,
    CURLOPT_TIMEOUT        => 120,
    CURLOPT_MAXREDIRS      => 10,
    CURLINFO_HEADER_OUT    => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_HTTP_VERSION   => CURL_HTTP_VERSION_1_1
);

我尽力解释我的问题,我为语言错误道歉

0 个答案:

没有答案