Make request on a page with Guzzle

0

I'm having trouble placing a POST request on a site through the Guzzle component. The target site is: link

He even enters the site but no results appear. I do not know if the problem is in HOW I make the request or if it is the PARAMETERS that I pass.

Someone could tell you what's wrong

Code:

<?php
use GuzzleHttp\Client; 
$client = new Client();
        //Padrão para as requisições
        $headers = [
            'headers' =>
                [
                    'User-Agent' => 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36',
                    'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                    'Accept-Language' => 'pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4',
                    'Accept-Encoding' => 'gzip, deflate',
                    'Referer' => 'http://ciagri.iea.sp.gov.br/nia1/subjetiva.aspx?cod_sis=1&idioma=1',
                    'Origin' => 'http://ciagri.iea.sp.gov.br',
                    'Host' => 'ciagri.iea.sp.gov.br',
                    'Connection' => 'keep-alive'
                ]
        ];
        //Parâmetros para a primeira requisição
        $body = [
            'body' =>
                [
                    'cmbAno_Inicial' => '1983',
                    'cmbAno_Final'=>'2014',
                    'cmbRegiao'=>'EDR',
                    'chkRegiao$0'=>'on',
                    'chkPerg$69'=>'on',
                    'imgPesquisar.x'=>'67',
                    'imgPesquisar.y'=>'20',
                    'cmbTpSaida'=>'RA'

                ]
        ];

        //Primeira requisição, passamos a consulta
        $param = array_merge($body, $headers);
        $request = $client->createRequest('POST', 'http://ciagri.iea.sp.gov.br/nia1/subjetiva.aspx?cod_sis=1&idioma=1', $param);
        $retorno=$client->send($request);
        $corpo=$retorno->getBody();
        echo $corpo;
    
asked by anonymous 29.05.2015 / 20:05

1 answer

1

Several parameters are missing.

Have you reached this list of parameters by looking at the HTML form? This method is laborious and subject to failure. I recommend doing this by looking at the Chrome Developer Tools Network tab or similar in other browsers.

From what I've seen, the parameters are missing:

__EVENTTARGET
__EVENTARGUMENT
__LASTFOCUS
__VIEWSTATE
__EVENTVALIDATION
txtAgrupamento

Note that some of these parameters (__VIEWSTATE and __EVENTVALIDATION, at first) appear to be a sort of CSRF token.

This means that you will have to make a GET request from the query page first, to get those parameters that are generated there. Extract these parameters from the HTML and include in your next POST request - this will bring up the query results.

    
30.05.2015 / 19:50