Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question