Curl Post Office Tracking

Asked

Viewed 2,509 times

3

I’m trying to make a POST with Curl to check my deliveries. But it’s not returning anything. Someone can help me?

<?php    
$post = array('Objetos' => 'PN752805878BR');

// iniciar CURL
$ch = curl_init();
// informar URL e outras funções ao CURL
curl_setopt($ch, CURLOPT_URL, 'http://www2.correios.com.br/sistemas/rastreamento');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($post));
// Acessar a URL e retornar a saída
$output = curl_exec($ch);
// liberar
curl_close($ch);
// Imprimir a saída
echo $output;    
?>
  • http://sooho.com.br/2017/03/24/rastreamento-de-pedidos-correios-php-soap/

1 answer

2

This Curl was made to not work, actually the problems:

  1. Your call is to the wrong page, /sistemas/rastreamento, when accessing the page by browser and "track" any order, you make a request to another page, /sistemas/rastreamento/resultado.cfm?.

    This second is the correct page, at least it is who was made to receive the form data.

  2. The website requires you to send a Referer and would be ideal, though not necessary, define a User-Agent.


That would be enough:

$post = ['objetos' => 'PN752805878BR', 'btnPesq' => 'Buscar'];

$ch = curl_init('http://www2.correios.com.br/sistemas/rastreamento/resultado.cfm?');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_REFERER, 'http://www2.correios.com.br/sistemas/rastreamento/');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

echo $output = curl_exec($ch);

If you don’t want to use the CURLOPT_USERAGENT and the CURLOPT_REFERER you can directly use the CURLOPT_HTTPHEADER, which I personally prefer. Adding other fixes such as protocol limitation and support for data compression could use:

$post = ['objetos' => 'PN752805878BR', 'btnPesq' => 'Buscar'];

$ch = curl_init('http://www2.correios.com.br/sistemas/rastreamento/resultado.cfm?');

curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Referer: http://www2.correios.com.br/sistemas/rastreamento/',
        'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'
    ],
    CURLOPT_POSTFIELDS => $post,
    CURLOPT_ENCODING => '',
    CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS
]);

echo $output = curl_exec($ch);

After testing remove the echo not to display the result on the page, and use the $output as you wish. Either way this is not totally safe, the website that is connecting does not support HTTPS, which exposes to various problems.

  • 1

    The second one worked perfectly. Thank you very much, if I want to put the product code in the URL, it would work too, Is there any way to do this?

  • I don’t understand which URL you want to put it in. If you want to get information from your URL just use $_GET['nome_da_variavel'] and then you can define $post['objetos'] = $_GET['nome_da_variavel']. Now if your question is Curl, you want to connect in Post using the parameter in the URL, instead of in the body, I believe that this is not possible, at least I believe that it will not work. Are two separate issues.

Browser other questions tagged

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