Curl post tracking

2

I'm trying to do a POST with curl to check my deliveries. But nothing is coming back. Can anyone 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;    
?>
    
asked by anonymous 30.06.2017 / 14:30

1 answer

1

This curl is meant to not work, actually the problems:

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

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

  • 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 do not want to use Referer and CURLOPT_USERAGENT you can directly use CURLOPT_REFERER , which I personally prefer. Adding other fixes such as limiting protocols 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 CURLOPT_HTTPHEADER to not show the result on the page, and use echo as you want. In either case this is not entirely secure, the site you are connecting to does not support HTTPS, which exposes you to various issues.

        
    30.06.2017 / 15:26