N
N
Nurdaulet Maksutov2021-02-12 09:09:39
PHP
Nurdaulet Maksutov, 2021-02-12 09:09:39

How to access other pages with authorized cURL?

I logged into the site with the following code. But how now to go to other pages authorized. How can I set these cookies on the second curl request?

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $login_url ); 
curl_setopt($ch, CURLOPT_POST, 1 ); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');  
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
$authed = curl_exec($ch); 
curl_close($ch);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
netrox, 2021-02-12
@maksutovn

More or less like this:

$cookieJar = tempnam('/tmp', 'cookie');

// Получаем cookie
$ch = curl_init($loginUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieJar);
$page = curl_exec($ch);

// Авторизованный запрос
$ch = curl_init($protectedUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieJar);
$page = curl_exec($ch);

curl_close($ch);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question