• PHP cURL 使用cookie 模拟登录


    cURL是什么

    cURL: http://php.net/manual/zh/book.curl.php

    PHP 支持 Daniel Stenberg 创建的 libcurl 库,能够连接通讯各种服务器、使用各种协议。libcurl 目前支持的协议有 http、https、ftp、gopher、telnet、dict、file、ldap。 libcurl 同时支持 HTTPS 证书、HTTP POST、HTTP PUT、 FTP 上传(也能通过 PHP 的 FTP 扩展完成)、HTTP 基于表单的上传、代理、cookies、用户名+密码的认证。

    这些函数在 PHP 4.0.2 中引入。

    使用cookie模拟登录,来查看需登录后才能浏览的页面 (get方式)

    set_time_limit(0);
    //目标url
    $url = "http://www.aa.com/index.php/Home/task/add";
    //使用的cookie,路径自己修改
    $cookie_file = __DIR__ . "/".'cookies.txt';
    $cookie_file = realpath($cookie_file);
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); //使用上面获取的cookies
    $response = curl_exec($ch);
    curl_close($ch);
    echo $response;
    View Code

    使用cookie模拟post提交请求

    set_time_limit(0);
    //目标url
    $url = 'http://www.aa.com';
    //post查询条件
    $fields = 'claimType=01&orderBy=1&pageSize=300&page.webPager.action=refresh&page.webPager.pageInfo.totalSize=8000&page.webPager.pageInfo.pageSize=300&page.webPager.currentPage=1';
    //cookie文件
    $cookie_file = __DIR__ . "/".'cookies.txt';
    $cookie_file = realpath($cookie_file);
    
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 60,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_COOKIEFILE => $cookie_file, 
        CURLOPT_COOKIEJAR => $cookie_file,
        CURLOPT_HTTPHEADER => array(
            "accept: */*",
            "accept-encoding: gzip, deflate",
            "accept-language: zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4,ja;q=0.2",
            "cache-control: no-cache",
            "connection: keep-alive",
            "content-type: application/x-www-form-urlencoded",
            //"cookie: $cookie",
            "origin: http://www.**.com",
            "pragma: no-cache",
            "referer: http://www.****.com",
            "user-agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36",
            "x-requested-with: XMLHttpRequest"
        ),
    ));
    
    //url
    curl_setopt($curl, CURLOPT_URL, $url);
    //post fields
    curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
    
    $response = curl_exec($curl);
    $err = curl_error($curl);
    $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    echo $response;
    View Code

    注意:在测试的时候,用火狐浏览器导出cookie为txt文件时,如果有些cookie项是关闭浏览器就失效,则导出的cookie文件中会缺失过期时间这一项,导致使用curl时功能不正常。所以在cookie文件生成后,可先检查下项。

    如,下面是缺失的

    www.qlm.com    FALSE    /    FALSE    PHPSESSID    as6if5c6d5ts23nrsgbq0a94b7
    www.qlm.com    FALSE    /    FALSE    1482396897    uname    %E7%AE%A1%E7%90%86%E5%91%98
    View Code

    可以手动补全,变成下面这样

    www.qlm.com    FALSE    /    FALSE    0    PHPSESSID    as6if5c6d5ts23nrsgbq0a94b7
    www.qlm.com    FALSE    /    FALSE    1482396897    uname    %E7%AE%A1%E7%90%86%E5%91%98
    View Code
  • 相关阅读:
    nginx location指令详解
    nginx日志模块、事件模块
    nginx核心模块常用指令
    Thinkphp <= 5.0.10 缓存导致的Getshell
    Thinkphp 3.2.3 bind注入 update/insert
    Thinkphp 3.2.3 where注入 select/delete
    Thinkphp 3.2.3 parseWhere设计缺陷导致的exp注入 (三)
    Thinkphp 3.2.3 parseWhere设计缺陷导致的exp注入 (二)
    Thinkphp 3.2.3 parseWhere设计缺陷导致的exp注入 (一)
    PHP POP 链
  • 原文地址:https://www.cnblogs.com/shenxinpeter/p/6207616.html
Copyright © 2020-2023  润新知