See third party pages

Asked

Viewed 254 times

3

  • Your question is a little confusing. You want to simulate a POST using PHP?

  • Download in that direction?

  • Yes simulate the post using php. Download in order to save the page code in a variable for example

  • Want to simulate a POST on that page and return the page code?

  • @Jorgeb. Exact .

1 answer

2


You can submit a request for POST using cURL;

First Step

Let’s check if he’s installed in his php.ini check that line:

;extension=php_curl.dll

If commented, take the ;.

Basic Use

  // Inicializamos o cURL informando um site;
  $requisicao = curl_init('www.seusite.com.br');
  // Definimos que deverá retornar o resultado;
  curl_setopt($requisicao, CURLOPT_RETURNTRANSFER, true);
  //Executa e salva o conteúdo na variável;
  $resultado = curl_exec($requisicao);
  // Encerramos a conexão;
  curl_close($requisicao);

Using POST

  // 'Input' => 'Valor'
  $parametros = [
    'id' => 1,
    'relacao_id' => 234
  ];
  // Setamos POST como true
  curl_setopt($requisicao, CURLOPT_POST, true);
  // Parâmetros que serão enviados pelo POST [array]
  curl_setopt($requisicao, CURLOPT_POSTFIELDS, $parametros);

Source: http://blog.thiagobelem.net/tutorial-basico-de-curl-instalacao-configuracao-e-uso/


Example of Curl informing headers to retrieve the captcha from the IRS website:

$ch = curl_init("http://www.receita.fazenda.gov.br/aplicacoes/atcta/cpf/captcha/gerarCaptcha.asp");
$options = [
    CURLOPT_COOKIEJAR => 'cookiejar',
    CURLOPT_HTTPHEADER => [
        "Pragma: no-cache",
        "Origin: http://www.receita.fazenda.gov.br",
        "Host: www.receita.fazenda.gov.br",
        "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:32.0) Gecko/20100101 Firefox/32.0",
        "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.5,en;q=0.3",
        "Accept-Encoding: gzip, deflate",
        "Referer: http://www.receita.fazenda.gov.br/aplicacoes/atcta/cpf/ConsultaPublica.asp",
        "Cookie: $cookie",
        "Connection: keep-alive"
    ],
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_FOLLOWLOCATION => 1,
    CURLOPT_BINARYTRANSFER => TRUE
];

curl_setopt_array($ch, $options);
$img = curl_exec($ch);
curl_close($ch);

Example using the API Guzzle for consultation of CEP:

$client = new Client();
$urlCurl = 'http://www.buscacep.correios.com.br/servicos/dnec/consultaEnderecoAction.do';
$request = $client->createRequest('POST', $urlCurl, [
    'body' => [
        'relaxation' => $param,
        'tipoCep' => 'ALL',
        'semelhante' => 'N',
        'cfm' => 1,
        'Metodo' => 'listaLogradouro',
        'TipoConsulta' => 'relaxation',
        'StartRow' => 1,
        'EndRow' => 100
    ],
    'headers' => [
        'Host' => 'www.buscacep.correios.com.br',
        'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36',
        'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;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://www.buscacep.correios.com.br/',
        'Connection' => 'keep-alive'
    ],
]);
$body = $client->send($request)->getBody();

Documentation: http://php.net/manual/en/book.curl.php
Other Examples: http://php.net/manual/en/curl.examples-basic.php

Guzzle is an API that helps you in the requisition processes, in the link beside you can find the documentation, usage examples and other requirements for the correct functioning.

  • @Jorgeb. I changed it :)

  • @Rafaelwithoeft I think he’s saying that curl_setopt($cURL... should be curl_setopt($requisicao...

  • 1

    Andre and Jorge, sorry for the mistake. It’s already been fixed

  • @Rafaelwithoeft Thank you

  • @Rafaelwithoeft The code worked,?

  • @Rodolfooliveira The result returned in the variable as you expected? What is missing is to be able to capture from the variable what you need, this is it?

  • @Rafaelwithoeft Returned an html page,ie,what I expected,however the page that returned is not the end of the search but the beginning of the search. To better understand enter this site: http://www.ciiagro.sp.gov.br/ciiagroonline/Listagens/BH/LBalancoHidricoEDR.asp and make the query(there are only two screens). In my post I am passing the parameters: $data = array( 'EDR'=>'Araraquara', 'Start'=>'16%2F2%2F2015', 'Query'=>'Query' ),if it’s not too much to analyze and see what I’m missing, because I didn’t find the error

  • @Rodolfooliveira I’ll do some tests

  • @Rafaelwithoeft Thank you Friend,.

  • @Rodolfooliveira Let’s go continue this discussion in chat.

  • @Rodolfooliveira disponibilizei aqui -> http://pastebin.com/xbZD5PsE the code is working, use Guzzle;

Show 6 more comments

Browser other questions tagged

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