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

618 views Asked by At

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://portal.agrinet.ie/) 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://portal.agrinet.ie/Exports.aspx
    [defaultNamespacePrefix:Symfony\Component\DomCrawler\Crawler:private] => default
    [namespaces:Symfony\Component\DomCrawler\Crawler:private] => Array
        (
        )

    [baseHref:Symfony\Component\DomCrawler\Crawler:private] => http://portal.agrinet.ie/Exports.aspx
    [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://portal.agrinet.ie/Default.aspx');
$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.

1

There are 1 answers

0
adnanoswd On BEST ANSWER

Solution:

$form = $exportPage->selectButton('ctl00$MainContent$btnDownload')->form();

$client->submit($form);

print_r($client->getResponse()->getContent());

getContent() gave me all the content of the file and I saved that content in another file which I had already created on my local machine.