【问题标题】:Cutoff body content from Guzzle response来自 Guzzle 响应的截断正文内容
【发布时间】:2019-06-25 23:54:41
【问题描述】:

我正在使用 Guzzle 登录页面,然后解析 DOM 以获取下载链接。 但是,登录后我不会收到完整的 DOM。带有下载链接的 HTML 即将在 DOM 字符串中开始,然后中断。

有人知道这可能是什么原因吗?

该页面在登录后,不可公开访问。

注意:我无法共享 URL 或登录数据,因此很可能无法复制问题。

这是 DOM 的结束

</SCRIPT>
  <TABLE   ALIGN=LEFT CELLSPACING=0 CELLPADDING=1 style='WIDTH:99%;max-width:1000px;'>                              

(之后什么都没有,但应该是,它只是不在响应中)

PHP:7.1.26

狂吃:6.3.3

一些代码,如果有帮助的话:

$response = self::$client->get(self::getConfig()['baseurl'] . '/' . parse_url($mainScreenUri)['path'], [
                'query'   => $query_params,
                'sink'    => date('Y.m.d_H-i-s') . '_sink_.txt',
                'debug'   => TRUE,
                'headers' => [
                    'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
                    'Accept'     => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
                    'Host'       => 'snip',
                ]
            ]
        );

        $x = $response->getBody()->__toString();
        file_put_contents(date('Y.m.d_H-i-s') . '.txt', $x);

由此创建的两个文件都被剪切并且不显示完整的正文。

响应调试:

* Found bundle for host snip: 0x5625c0ab6100 [can pipeline]
* Re-using existing connection! (#0) with host snip
* Connected to snip port 443 (#0)
> GET snip HTTP/1.1
Host: snip
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Cookie: snip

< HTTP/1.1 200 OK
< Date: Tue, 25 Jun 2019 12:55:56 GMT
< Server: Apache/2.4.7 (Ubuntu)
< X-Powered-By: PHP/5.5.9-1ubuntu4.26
< X-Frame-Options: sameorigin
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
< Pragma: no-cache
< Vary: Accept-Encoding
< Transfer-Encoding: chunked
< Content-Type: text/html
< 
* Curl_http_done: called premature == 0
* Connection #0 to host snip left intact

编辑 使用流一次只获取几个字节我有同样的问题。

/** @var \GuzzleHttp\Promise\Promise $promise */
        $promise = self::$client->getAsync(self::getConfig()['baseurl'] . '/' . parse_url($mainScreenUri)['path'], [
                'query'           => $query_params,
                'sink'            => 'snip' . date('Y.m.d_H-i-s') . '_sink_.txt',
                'debug'           => $resource,
                'stream'          => TRUE,
                'headers'         => [
                    'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
                    'Accept'     => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
                    'Host'       => 'snip',
                    //                    'Referer'    => 'snip/popup.php?user=' . self::getConfig()['username'] . '&pwi=' . $pwi . '&pwh=' . $hpw,
                ],
                'allow_redirects' => [
                    'max' => 50,
                ]
            ]
        );

        /** @var \GuzzleHttp\Psr7\Response $response */
        $response = $promise->wait();

        /** @var \GuzzleHttp\Psr7\Stream $body */
        $body = $response->getBody();

        $dataRead = "";
        while (!$body->eof()) {
            $data     = $body->read(1024);
            $dataRead .= $data;
        }

$dataRead 与其他所有内容一样被截断。

【问题讨论】:

  • 如果我理解你是正确的,你想将页面加载到某个特定的地方并停止加载。我认为您需要从 http 套接字读取。
  • 不,我不想部分加载页面。它必须完全加载,这样我才能得到完整的身体。部分身体对我没有帮助,而且是不受欢迎的。

标签: php guzzle guzzle6


【解决方案1】:

我发现了问题。这是一个损坏的参数,服务器决定返回损坏的 HTML 而不是错误消息或什么都没有。

【讨论】:

    猜你喜欢
    • 2013-10-23
    • 1970-01-01
    • 2021-06-18
    • 2018-07-06
    • 2015-04-27
    • 2017-11-01
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多