mardi 29 août 2017

how to save the download file locally from a asp.net form using goutte

I am trying to create a PHP script using goutte to

  • Login
  • Click on submit button.
  • Save the csv file locally which gets downloaded(if done manually).

This app(http://ift.tt/2iHI6Vq) is built on asp.net

I am able to log in successfully and hit the submit button too on the form through the code but it currently returns only this result:

Symfony\Component\DomCrawler\Crawler Object
(
    [uri:protected] => http://ift.tt/2gpUd8H
    [defaultNamespacePrefix:Symfony\Component\DomCrawler\Crawler:private] => default
    [namespaces:Symfony\Component\DomCrawler\Crawler:private] => Array
        (
        )

    [baseHref:Symfony\Component\DomCrawler\Crawler:private] => http://ift.tt/2gpUd8H
    [document:Symfony\Component\DomCrawler\Crawler:private] => 
    [nodes:Symfony\Component\DomCrawler\Crawler:private] => Array
        (
        )

    [isHtml:Symfony\Component\DomCrawler\Crawler:private] => 1
)

This is the php code:

require 'vendor/autoload.php';
use Goutte\Client;

$client = new Client();
echo '<pre>';
$crawler = $client->request('GET', 'http://ift.tt/2iHI7Zu');
$form = $crawler->selectButton('ctl00$MainContent$btnLogin')->form();

$crawler = $client->submit($form, array('ctl00$MainContent$txtUsername' => 'XXXX', 'ctl00$MainContent$txtPassword' => 'XXXXX'));
  
$id = $crawler->filter("#ctl00_mnuMainn26 a")->link();
$exportPage = $client->click($id);
$downloadBut = $exportPage->selectButton('ctl00$MainContent$btnDownload')->form();
$file = $client->submit($downloadBut);
print_r($file);

My question is how I can save a file locally after clicking the submit button using goutte.




Aucun commentaire:

Enregistrer un commentaire