如何将以前cURL中的所有cookie添加到下一个cURL

时间:2016-08-08 21:43:49

标签: php cookies

我有一个PHP文件,它向网页发送cURL请求,获取一些应该设置的cookie,然后设置cookie以执行下一个cURL请求:

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

我不确定如何分离所有Cookie,然后自行获取名称和值。我该怎么做?

1 个答案:

答案 0 :(得分:1)

将这些饼干放入饼干罐中:

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