Save cookies from one request and use on another then cURL

0

I am trying to login to a site and save the cookies to access other pages, I found the 'CURLOPT_COOKIEJAR' option that saves the cookies in a NETSCAPE file, the question is how would a second request be made to another URL using other parameters POST, using the same $ch , what is the best way to proceed?

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://site.com.br');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'login=usuario&senha=123456');
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookies.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$content = curl_exec($ch);


curl_close($ch);
    
asked by anonymous 28.05.2018 / 07:32

1 answer

1

Use CURLOPT_COOKIEFILE "pointed" to the same file.

//...
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookies.txt');
//...

cookies.txt will have cookies written by JAR and will be read by FILE .

    
28.05.2018 / 12:18