You can send a request of POST
using cURL
;
First Step
Let's check if it is installed, in your php.ini check this line:
;extension=php_curl.dll
If it is commented out, take out ;
.
Basic Usage
// 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: link
Example of cURL reporting 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 Guzzle API for the CEP query:
$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 : link
Other Examples: link
Guzzle is an API that helps you in the requisition process, in the link to the side you can find the documentation, examples of use and other requirements for the correct operation.