1

My first post here... I am handling cookies between get/post pair of requests.

Based on this other question: share the same cookie between two website using PHP cURL extension

Is my approach correct?

<?php
//set POST variables
$referer = 'http://www.correios.com.br/encomendas/prazo/';
$url = 'http://www.correios.com.br/encomendas/prazo/prazo.cfm';
$fields = array(
           'Altura' => '8',
                        'Comprimento'   => '16',
                        'Formato'   => '1',
                        'Largura'   => '15',
                        'MaoPropria'    => 'N',
                        'avisoRecebimento'  => 'N',
                        'cepDestino'    => '99999999',
                        'cepOrigem' => '99999999',
                        'data'  => '02/12/2012',
                        'dataAtual' => '02/12/2012',
                        'embalagem' => '',
                        'peso'  => '1',
                        'resposta   paginaCorreios'=> '',
                        'servico'   => '99999',
                        'valorD'    => '',
                        'valorDeclarado'=> ''   
        );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, '10');
curl_setopt($ch, CURLOPT_AUTOREFERER, true);


curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');


// Post 1
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_URL, $referer);

$result = trim(curl_exec($ch));
//

$ch = curl_init($url);

//Post 2
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_REFERER, $referer);

//execute post
$result = curl_exec($ch);
//

//close connection
curl_close($ch); 

echo($result);
?>

What I need is mimic this behavior:

Response Headers:
Content-Type    text/html; charset=ISO-8859-1
Date    Sun, 02 Dec 2012 20:13:08 GMT
Server  Microsoft-IIS/7.5
Set-Cookie  JSESSIONID=c6308c7edb989a54edef1b656119101c321b;path=/ CFGLOBALS=urltoken%3DCFID%23%3D33736896%26CFTOKEN%23%3D28355865%26jsessionid%23%3Dc6308c7edb989a54edef1b656119101c321b%23lastvisit%3D%7Bts%20%272012%2D12%2D02%2018%3A13%3A09%27%7D%23timecreated%3D%7Bts%20%272012%2D12%2D02%2002%3A13%3A29%27%7D%23hitcount%3D6%23cftoken%3D28355865%23cfid%3D33736896%23;expires=Tue, 25-Nov-2042 20:13:09 GMT;path=/
Transfer-Encoding   chunked

Request Headers:
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3
Connection  keep-alive
Cookie  CFID=33736896; CFTOKEN=28355865; CFGLOBALS=urltoken%3DCFID%23%3D33736896%26CFTOKEN%23%3D28355865%26jsessionid%23%3D983074629e3d7344ff534b12637b7f127163%23lastvisit%3D%7Bts%20%272012%2D12%2D02%2002%3A56%3A48%27%7D%23timecreated%3D%7Bts%20%272012%2D12%2D02%2002%3A13%3A29%27%7D%23hitcount%3D5%23cftoken%3D28355865%23cfid%3D33736896%23
Host    www.correios.com.br
User-Agent  Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:17.0) Gecko/20100101 Firefox/17.0

Reading from some others topics, I have learned about multi_exec: PHP cURL multi_exec delay between requests

I mean, what is the correct approach for the this task?

Thanks in advance! Renato

Community
  • 1
  • 1
Renato Aloi
  • 300
  • 2
  • 9
  • 1
    Don't make a new curl `$ch = curl_init($url);`, use the one that did the first request. Other than that, what's your question? The answer you linked is about a different thing. There he emulates google login. You access some login form directly. – Ranty Dec 03 '12 at 19:46
  • Owww, I see now about the new curl init... That's what I need! Thanks! – Renato Aloi Jul 29 '14 at 15:30
  • It's never too late, is it?:) – Ranty Jul 29 '14 at 16:14

0 Answers0