Guzzle 模拟登录 cookie 使用

作者: JONE 分类: 编程 发布时间: 2020-11-20 19:00

参考文档 https://guzzle-cn.readthedocs.io/zh_CN/latest/quickstart.html#cookies

此处以cnzz的查看密码统计为例,抓取当前网站的在线数据

一,对单个请求设置cookei,每个请求需要单独设置cookie,否则不带cookie去请求

<?php

include './vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;

$url = 'https://uweb.umeng.com/v1/login.php?t=login&siteid=127927515*';  // 登录操作

$client = new Client();

$jar = new CookieJar;

$r = $client->request('POST', $url, [
    //'allow_redirects' => false,
    'cookies' => $jar,
    'form_params' => ['password' => '123654'],
    'headers' => [
        'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
        'Accept'     => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'content-type'=>'application/x-www-form-urlencoded'
    ]
]);

$cookie =$jar->toArray(); // 获取 登录成功的cookie ,此数据可以存起来,下次无需进行登录操作直接使用

$jar2 = (new CookieJar(true,$cookie));  // 使用已有的cookie数据

// 当前在线页面的实时数据
$r2 = $client->request('GET', 'https://web.umeng.com/main.php?siteid=1279275151&c=flow&a=realtime&ajax=module=flash&type=Pie', [
    'cookies' => $jar2,  
    'allow_redirects' => false,

    'headers' => [
        'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
    ]
]);

var_dump($r2->getBody()->getContents());
// 打印内容如下
//string(211) "{"data":{"time":["2020-11-20 18:22:55","2020-11-20 18:08:55"],"flash":{"data":{"numStart":0,"numEnd":7,"High":23,"total_ip":70,"total_pv":143,"total_uv":114,"Time":"17:00:00","Old":[1.75,2],"New":[98.25,112]}}}}"

二、全局设置cookie,无需为每个请求设置cookie,Guzzle会全局维护cookie数据

<?php

include './vendor/autoload.php';

use GuzzleHttp\Client;

$url = 'https://uweb.umeng.com/v1/login.php?t=login&siteid=127927515*';  // 登录操作


$client = new Client(['cookies' => true]);

$r = $client->request('POST', $url, [
    //'allow_redirects' => false,
    'form_params' => ['password' => '123654'],
    'headers' => [
        'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
        'Accept'     => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'content-type'=>'application/x-www-form-urlencoded'
    ]
]);

// 当前在线页面的实时数据
$r2 = $client->request('GET', 'https://web.umeng.com/main.php?siteid=1279275151&c=flow&a=realtime&ajax=module=flash&type=Pie', [
    'allow_redirects' => false,

    'headers' => [
        'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
    ]
]);

var_dump($r2->getBody()->getContents());

// 打印内容如下
//string(211) "{"data":{"time":["2020-11-20 18:22:55","2020-11-20 18:08:55"],"flash":{"data":{"numStart":0,"numEnd":7,"High":23,"total_ip":70,"total_pv":143,"total_uv":114,"Time":"17:00:00","Old":[1.75,2],"New":[98.25,112]}}}}"

另 还有就是从浏览器提取出来的cookie数据使用