使用cURL在两个PHP请求之间共享相同的cookie

时间:2012-12-02 21:17:11

标签: php post cookies curl get

我在这里发表的第一篇文章......我正在处理获取/发布请求之间的cookie。

基于这个问题: share the same cookie between two website using PHP cURL extension

我的方法是否正确?

<?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);
?>

我需要的是模仿这种行为:

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

从其他一些主题中读到,我已经了解了multi_exec: PHP cURL multi_exec delay between requests

我的意思是,这项任务的正确方法是什么?

提前致谢! 雷纳托

0 个答案:

没有答案