Request on a page with Guzzle

Asked

Viewed 495 times

0

I’m having trouble making a POST request on a website through the component Guzzle. The target site is :http://ciagri.iea.sp.gov.br/nia1/subjetiva.aspx?cod_sis=1&idioma=1

He even enters the site but no results appear. I don’t know if the problem is HOW I make the request or if it is the PARAMETERS what a step.

Would anyone know 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;

1 answer

1


Several parameters are missing.

Have you arrived at this list of parameters looking at the HTML form? This method is laborious and subject to failures. I recommend doing this by looking at the Network tab of the Chrome Developer Tools 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 kind of CSRF token.

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

  • could you give me an example? Because in the url itself there are already parameters that are obtained by GET

  • Even with these GET parameters of the URL, you are missing the ones I mentioned. Look at the source code of the query page, note that there is <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="..." />, besides the others I’ve listed for you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.