title: Share the same cookie between two PHP requests with cURL
body: <p>My first post here... I am handling cookies between get/post pair of requests.</p>
  
  <p>Based on this other question:
  <a href="https://stackoverflow.com/questions/2845684/share-the-same-cookie-between-two-website-using-php-curl-extension">share the same cookie between two website using PHP cURL extension</a></p>
  
  <p>Is my approach correct?</p>
  
  <pre><code>&lt;?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' =&gt; '8',
                          'Comprimento'   =&gt; '16',
                          'Formato'   =&gt; '1',
                          'Largura'   =&gt; '15',
                          'MaoPropria'    =&gt; 'N',
                          'avisoRecebimento'  =&gt; 'N',
                          'cepDestino'    =&gt; '99999999',
                          'cepOrigem' =&gt; '99999999',
                          'data'  =&gt; '02/12/2012',
                          'dataAtual' =&gt; '02/12/2012',
                          'embalagem' =&gt; '',
                          'peso'  =&gt; '1',
                          'resposta   paginaCorreios'=&gt; '',
                          'servico'   =&gt; '99999',
                          'valorD'    =&gt; '',
                          'valorDeclarado'=&gt; ''   
          );
  
  //url-ify the data for the POST
  foreach($fields as $key=&gt;$value) { $fields_string .= $key.'='.$value.'&amp;'; }
  rtrim($fields_string, '&amp;');
  
  //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);
  ?&gt;
  </code></pre>
  
  <p>What I need is mimic this behavior:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Reading from some others topics, I have learned about multi_exec:
  <a href="https://stackoverflow.com/questions/6987404/php-curl-multi-exec-delay-between-requests">PHP cURL multi_exec delay between requests</a></p>
  
  <p>I mean, what is the correct approach for the this task?</p>
  
  <p>Thanks in advance!
  Renato</p>