我用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文件?
答案 0 :(得分:0)
我修改了您的代码以在本地进行测试 * ,它按预期工作,因此我认为代码不是问题所在。您最好的选择是与网站相关联的人,但是您可以尝试以下几项操作来解决问题:
cookie.txt
并检查是否已使用您在响应中看到的内容填充来验证是否存储了cookie 。* 我用非常简单的this guide for Firefox(而不是您帖子中的确切网址)测试了您的cURL选项。