【问题标题】:Download pdf file from curl while url is redirectingurl重定向时从curl下载pdf文件
【发布时间】:2020-07-13 15:53:48
【问题描述】:

我用 PHP 创建了一个脚本。它登录一个网站,然后转到该网站上的发票列表,我想制作脚本来下载它们。一张发票的链接如下:

$newest_invoice_url= 
    https://secure3.billerweb.com/alw/inetSrv?client=701122300&type=ApplicationMenu&authTkn=r7mdbUaDHRqEWm4sbY1q40uvMhNQFjwDVOzhEZaAyFrGMyD0pTgCde/QHTSUf0YrJJw5PThCcAV8O7a2ipV5IZmi7jNGArIZ1ddy-WEJcuNAbMp0VRMnNO9VFCA7s78vwzB9VqCn/Qm5zfyzXH2YoNaqMATofgc3v-SSLYzzV3LGGVlvAF6TPS4JgPbcdxotOwJaGhPsM0qfoBiE74zECNA3o-x9-Amqa-/3Q1cRFUa3LtV77l/mDG1pQP1KbISD27TfSPvG1IgyzOz1BP6n0Ah4WksP95yAMhDUuaViKECBpC4n3n-SdyHyQpXz8UsPS8F3WWwj7aMwFvp2aaHRnP5j3uVl4gRGw1l3ee5BLmhkBZFdB57bcAP2Pinmk4krIuqvzjCZM780j9lMQ7E/lS-KqWQN/zrGF3JOg6CP3HYtna6Ne-XgseIxsf-Ecu7qKfZ-DRCSXtv1ulnn8PW1btvjgUeS-Aia8mo9T3CzUmVnbdkOG0JfrrT9mjHOUevqZb-48776CX9svPxujKVFjHELPX5E8bXzv2yIWyMoHfqpaVm1D2B2BP56GYwD5OQL0a5BNnsEjHBIpMMUCsGAbpXl0bABleBx&unitCode=ALW&keyTkn=W9EqaRvq0FQ_Ij9Nj1zRaPoc88pUKFGSOYlgLKY6qQxuLnHdhQ38Gq8CUviDeObpxvo46fbZ1XRjPTzMc0tTqCphM9Q79QjheglAX2Ay2Gzdo2r0KnRx9gBqoAyKSHk/93tuEmVAbszFdYfP0E-sJRCUWvnNUTDOe6TyEeblIzQ7wEDrNt4nmEI_&slchannel=ALWRSA&enc=web

这个https://secure3.billerweb.com 不是我登录的站点,这是一个重定向链接。所以我创建了这段代码:

    $ch = curl_init();
$source = $newest_invoice_url;
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt($ch, CURLOPT_HEADER,1);
curl_setopt($ch,          CURLOPT_FOLLOWLOCATION ,true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE,'cookie.txt');
              curl_setopt($ch, CURLOPT_HTTPHEADER , array(
              'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36',
             // 'accept: application/json',
              'accept-encoding: gzip, deflate, br',
              'accept-language: en-US,en;q=0.9',
              'Content-Type: application/pdf',
              $auth_token,
              ));
$data = curl_exec ($ch);
curl_close ($ch);

print_r($data);

从这个代码我收到这个:

HTTP/1.1 302 Moved Temporarily
Date: Mon, 13 Jul 2020 15:44:09 GMT
Content-length: 0
Content-type: text/html
Set-cookie: _stkn-5VxD.GHe=419dZioHkOUhg6RZcU844Q__; Path=/alw/inetSrv; HttpOnly; Secure; SameSite=None
Strict-transport-security: max-age=31536000
Cache-control: no-store,no-cache,max-age=0,must-revalidate
Pragma: no-cache
Content-security-policy: child-src 'self'; connect-src 'self' https://www.google-analytics.com; default-src 'self'; font-src 'self' https://fonts.gstatic.com; frame-ancestors 'self'; img-src * data: https://www.google-analytics.com; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://www.google-analytics.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com;
X-content-type-options: nosniff
X-xss-protection: 1; mode=block
Location: https://secure3.billerweb.com/alw/inetSrv?sessionHandle=5VxD/GHel0zjLaUDhStfaVGhnhkpviikX-aSlAWWrj1U7BauY2w9NzAxMTIyMzAwJlJlcXVlc3RUeXBlPVNob3dQZGY_&client=701122300&type=CompatPresentmentService&action=ShowPdf&firstPage=true

HTTP/1.1 200 OK
Date: Mon, 13 Jul 2020 15:44:09 GMT
Content-length: 1123
Content-type: text/html
Set-cookie: _stkn-5VxD.GHe=419dZioHkOUhg6RZcU844Q__; Path=/alw/inetSrv; HttpOnly; Secure; SameSite=None
Strict-transport-security: max-age=31536000
Cache-control: no-store,no-cache,max-age=0,must-revalidate
Pragma: no-cache
Expires: -1
Content-security-policy: child-src 'self'; connect-src 'self' https://www.google-analytics.com; default-src 'self'; font-src 'self' https://fonts.gstatic.com; frame-ancestors 'self'; img-src * data: https://www.google-analytics.com; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://www.google-analytics.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com;
X-content-type-options: nosniff
X-xss-protection: 1; mode=block

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>

                                                                                                              
<head><title></title>
                     
<LINK REL="StyleSheet" HREF="styles/mainStyle.css">
</head>

<body>
<div style="float: left; border: 1px solid black; background-color: #FFFFFF; padding: 5px">
    <div class="TitleFont">Operation failed</div>
    <div class="TitleFont">Reason</div>
    <div>
    <div class="custom-message-box">
                <div class="ErrorFont" ALIGN="left" >A server error has occurred.</div>
                <div class="ErrorFont" ALIGN="left" >Error reference id: YQL-99950</div>
                <div class="ErrorFont" ALIGN="left" >Time: Mon Jul 13 10:44:09 CDT 2020</div>
    </div>
    </div>
    <div style="width: 600px">
        <p class="form-style-text">
        If contacting customer support, please quote the above error reference id. You may be able to press the browser Back button to return to the previous screen. Otherwise you may need to login again. We apologize for the inconvenience.
        </p>
    </div>
</div>

</body>
</html>

所以我的代码运行不好,有没有人知道我应该怎么做才能成功下载这个 pdf 文件?

【问题讨论】:

    标签: php pdf session curl download


    【解决方案1】:

    我调整了您的代码以在本地进行测试* 并且它按预期工作,所以我不认为代码是问题所在。最好的办法是联系与该网站相关的人,但您可以先尝试以下几件事来解决问题:

    • 在您的浏览器中比较成功交换的 HTTP 日志(您在 reddit 上的帖子中提到过)以确定您的脚本是否缺少必要的 cookie、标头等。请参阅 this guide for Chrome 或 @ 987654322@ 如果您不熟悉。由于涉及重定向,您需要在 Chrome 中启用“保留日志”或在 Firefox 中启用“持久日志”。
    • 通过打开 cookie.txt 并检查文件是否填充了您在响应中看到的内容,验证是否正在存储 cookie。

    *我用一个非常简单的script that sets a cookie and redirects 测试了你的 cURL 选项,而不是你帖子中的确切 URL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-04
      相关资源
      最近更新 更多