卷曲登录网站,然后在登录时浏览不同的链接

时间:2018-08-22 11:11:05

标签: php curl

我正在使用curl登录网站:

$credentials = [
    'user' => 'username',
    'password' => 'passowrd',
];

curl_setopt($ch, CURLOPT_URL, 'https://shop.com/login.php');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($credentials));

$response = curl_exec($ch);

它可以登录,但是一旦登录,我需要浏览一些网址并抓取一些数据(仅在登录后可用):

$products = ['1', '2', '3'];
foreach ($products as $id) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://shop.com/product.php?id=' . $id);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);

    //do something with that response

    curl_close($ch);
}

是否有办法维持session在第一个curl请求中启动,以便我可以在foreach循环中使用它-这样我就可以抓取数据了?

谢谢!

1 个答案:

答案 0 :(得分:0)

在您的第一个请求中,您应该将cookie保存在变量中,并在其他请求中使用它。像这样:

$credentials = [
    'user' => 'username',
    'password' => 'passowrd',
];

curl_setopt($ch, CURLOPT_URL, 'https://shop.com/login.php');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($credentials));

$response = curl_exec($ch);

获取cookie并保存在$cookie变量中:

prog_match('/^Set-Cookie:\s*([^;]*)/mi',$response,$m);
parse_str($m[1],$cookie);

在其他请求中,您可以像这样使用它:

$products = ['1', '2', '3'];
foreach ($products as $id) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://shop.com/product.php?id=' . $id);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_COOKIE, 'PHPSESSID='.$cookie['PHPSESSID']);
    $response = curl_exec($ch);

    //do something with that response

    curl_close($ch);
}