TreeHouseFalcon TreeHouseFalcon - 3 months ago 33
PHP Question

How to add all cookies from previous cURL to next cURL

I have a PHP file that sends a cURL request to a web page, gets some cookies that should be set, and then sets the cookies in order to perform the next cURL request:

<?php
$ch = curl_init('https://www.roblox.com/newLogin');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
username => "EncodedLua",
password => "", // Hidden
submitLogin => "Log In"
));

curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $result, $matches);
$cookies = array();
$roblosecuritykey = "";

foreach($matches[1] as $item) {
parse_str($item, $cookie);
$cookies = array_merge($cookies, $cookie);

// Set all of the returned cookies
}

curl_close($ch);

$ch2 = curl_init('https://www.roblox.com/groups/api/change-member-rank');
curl_setopt($ch2, CURLOPT_POSTFIELDS, array(
groupId => 2902455,
newRoleSetId => 19777964,
targetUserId => 55203439
));
curl_setopt($ch2, CURLOPT_HEADER, 1);
$result2 = curl_exec($ch2);

curl_close($ch2);
?>


I'm just not sure how to separate all of the cookies and then get the names and values by themselves. How would I do this?

Answer

Just put those cookies in the cookie jar:

<?php
$ch = curl_init('https://www.roblox.com/newLogin');
curl_setopt($ch, CURLOPT_COOKIEJAR, "/home/me/mycookies");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    username => "EncodedLua",
    password => "", // Hidden
    submitLogin => "Log In"
));
Comments