开发者社区> 问答> 正文

怎么让登陆网站的时候带着cookie访问?

第一步:在我自己的本地(127.0.0.1) 模拟登陆一个网站 获取到cookie 保存在本地。
第二步:当我正常访问这个网站(http://xxxxxshequ.com) 带着我保存在本地的cookie
朋友做营运 切换很多小号 手动登陆 搞得很麻烦想让他轻松些 第一步实现起来很简单 主要是第二步有什么办法实现吗?

展开
收起
蛮大人123 2016-02-27 13:51:32 3929 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    基于 PHP/CURL 编写的类库, 使用方法见注释.
    直接使用, 不用处理Cookie(程序自动会处理, Cookie信息保存于调用类时传递的参数所指定的文件里).

    <?php
    
    /**
     * Author: dds_feng
     * Email: dds_feng@qq.com
     *
     * Example:
     * $http = new HttpClient(__DIR__.'/cookie.ck');
     * $http->SetReferer('http://foo.com');//set request referer
     * echo $http->Get('http://foo.com/');//get
     * $http->SetProxy('http://127.0.0.1:8888');//set http proxy
     * echo $http->Post('http://bar.com/xxx', array('a'=>'123', 'b'=>'456'));//post
     **/
    
    class HttpClient{
        private $ch;
    
        function __construct($cookieJar){
            $this->ch = curl_init();
            curl_setopt($this->ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36');//UA
            curl_setopt($this->ch, CURLOPT_TIMEOUT, 60);//timeout
            curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, TRUE);//follow redirection
            curl_setopt($this->ch, CURLOPT_AUTOREFERER, true);
            curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, FALSE);//ssl
            curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($this->ch, CURLOPT_COOKIEJAR, $cookieJar);//cookie jar
            curl_setopt($this->ch, CURLOPT_COOKIEFILE, $cookieJar);
        }
    
        function __destruct(){
            curl_close($this->ch);
        }
    
        final public function SetProxy($proxy='http://127.0.0.1:8888'){
            //curl_setopt($this->ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
            curl_setopt($this->ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);//HTTP proxy
            //curl_setopt($this->ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);//Socks5 proxy
            curl_setopt($this->ch, CURLOPT_PROXY, $proxy);
        }
    
        final public function SetReferer($ref=''){
            if($ref != ''){
                curl_setopt($this->ch, CURLOPT_REFERER, $ref);//Referrer
            }
        }
    
        final public function SetCookie($ck=''){
            if($ck != ''){
                curl_setopt($this->ch, CURLOPT_COOKIE, $ck);//Cookie
            }
        }
    
        final public function Get($url, $header=false, $nobody=false){
            curl_setopt($this->ch, CURLOPT_POST, false);//GET
            curl_setopt($this->ch, CURLOPT_URL, $url);
            curl_setopt($this->ch, CURLOPT_HEADER, $header);//Response Header
            curl_setopt($this->ch, CURLOPT_NOBODY, $nobody);//Response Body
            return curl_exec($this->ch);
        }
    
        final public function Post($url, $data=array(), $header=false, $nobody=false){
            curl_setopt($this->ch, CURLOPT_URL, $url);
            curl_setopt($this->ch, CURLOPT_HEADER, $header);//Response Header
            curl_setopt($this->ch, CURLOPT_NOBODY, $nobody);//Response Body
            curl_setopt($this->ch, CURLOPT_POST, true);//POST
            curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($data));//data
            return curl_exec($this->ch);
        }
        final public function getError(){
            return curl_error($this->ch);
        }
    }
    // vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4:
    2019-07-17 18:48:46
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
安全机制与User账户身份验证实战 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载