我想登录网页并下载文件。
到目前为止,我尝试了下面的代码。问题是我似乎不再获得授权了。下载的文件是login.html页面。
有人知道如何使这项工作?提前谢谢!
<?php
require 'vendor/autoload.php';
use Goutte\Client;
$client = new Client();
$crawler = $client->request('GET', 'https://website.com/login.php');
$form = $crawler->selectButton('Login')->form();
$crawler = $client->submit($form, array('username' => 'username', 'password' => 'password'));
...
$download_link = 'https://website.com/extracted_download_link_from_crawler.pdf';
$guzzleClient = $client->getClient();
$response = $guzzleClient->get($download_link, ['save_to' => '/local_path/file.pdf']);
答案 0 :(得分:1)
$cookieJar = $client->getCookieJar();
$guzzleClient = $client->getClient();
$jar = GuzzleHttp\Cookie\CookieJar::fromArray($cookieJar->all(), 'website.com');
$response = $guzzleClient->get('URL TO FILE', ['cookies' => $jar, 'sink' => 'my.pdf']);
答案 1 :(得分:0)
弄清楚自己:
我从Goutte客户端获取cookie并将它们存储在Guzzle Client cookiejar中:
//get the PHPSESSION COOKIE
$cookieJar = $goutteClient->getCookieJar();
$all_cookies = $cookieJar->all();
$PHPSESSID_value = $all_cookies[7]->getValue();
//Update the cookie for different guzzleClient and download
$guzzleClient = $client->getClient();
$jar = new \GuzzleHttp\Cookie\CookieJar;
$response = $guzzleClient->get($download_link, ['cookies' => $jar, 'save_to' => '/local_path/file.pdf']);