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.
Your question is a little confusing. You want to simulate a POST using PHP?
– André Ribeiro
Download in that direction?
– Rafael Withoeft
Yes simulate the post using php. Download in order to save the page code in a variable for example
– Rodolfo Oliveira
Want to simulate a POST on that page and return the page code?
– Jorge B.
@Jorgeb. Exact .
– Rodolfo Oliveira