zoukankan      html  css  js  c++  java
  • php curl cookie 读写

    普通 curl post 请求

    public static function curlPost($url, $post_fields = array(), $timeout = 5)
    {
        $timeout = intval($timeout);
        if ($timeout <= 0) {
            $timeout = 5;
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); //60秒
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_REFERER, '');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

    curl请求中设置 cookie

    public function curlPost($url, $params)
    {
        $myCurl = curl_init(); // 初始化一个curl对象
        curl_setopt($myCurl, CURLOPT_URL, $url); // 设置你需要抓取的URL
        curl_setopt($myCurl, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($myCurl, CURLOPT_POST, 1);
    
        curl_setopt($myCurl, CURLOPT_RETURNTRANSFER, 1); // 设置是将结果保存到字符串中还是输出到屏幕上,1表示将结果保存到字符串
        curl_setopt($myCurl, CURLOPT_HEADER, 0);
        curl_setopt($myCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($myCurl, CURLOPT_USERAGENT, 'CRM v1.0');
        
        if (!empty($params['sessionid'])) { // 设置cookie
            curl_setopt($myCurl, CURLOPT_COOKIE, 'JSESSIONID='.$params['sessionid'].'; PHPSESSID='.$params['sessionid']);
        }
        if(isset($params['requestHeaders'])) {
            curl_setopt($myCurl, CURLOPT_HTTPHEADER, $params['requestHeaders']);
            unset($params['requestHeaders']);
        }
        curl_setopt($myCurl, CURLOPT_POSTFIELDS, $params);
        $header = '';
        curl_setopt($myCurl, CURLOPT_HEADERFUNCTION, function($curl, $h) use (&$header) {
            $header = $header.$h;
            return strlen($h);
        });
        $re = curl_exec($myCurl); // 执行请求
        curl_close($myCurl); // 关闭url请求
        return $re;
    }

    在返回值中设置cookie (在服务端 登录 后台,再设置好cookie,返回 前端)

    方式1:

    public function actionLogin() {
        $url = Yii::$app->params['url'];
        $params = Yii::$app->request->post();
    
        $res = $this->curlPost($url, $params);
        $json = json_decode($res, true);
        
        $clientinfo = isset($json['results'][0]['clientinfo'])?urldecode($json['results'][0]['clientinfo']):"";
        $sessionid = isset($json['results'][0]['sessionid'])?urldecode($json['results'][0]['sessionid']):"";
        header("Set-Cookie: clientinfo=$clientinfo; domain=baidu.com; path=/", false);
        header("Set-Cookie: SESSIONID=$sessionid; path=/", false);
        
        $this->jsonReturn($json);
    }

    方式2:

    public function actionLogin() {
        $url = Yii::$app->params['url'];
        $params = Yii::$app->request->post();
    
        $res = $this->curlPost($url, $params);
        $json = json_decode($res, true);
    
        $clientinfo = isset($json['results'][0]['clientinfo'])?urldecode($json['results'][0]['clientinfo']):"";
        $sessionid = isset($json['results'][0]['sessionid'])?urldecode($json['results'][0]['sessionid']):"";
        setcookie("clientinfo", $clientinfo);
        setcookie("SESSIONID", $sessionid);
    
        $this->jsonReturn($json);
    }
  • 相关阅读:
    B. Connecting Universities DFS,无向树
    HDU 5808 Price List Strike Back bitset优化的背包。。水过去了
    uva 6910
    HDU 5778 abs 数学
    Invitation Cards POJ 1511 SPFA || dij + heap
    HDU 2243 考研路茫茫——单词情结 求长度小于等于L的通路总数的方法
    cmd链接mysql
    如何设置远程访问mysql
    ERROR 1045 (28000): Access denied for user'root'@'localhost'(using password:YES)51Testing软件测试网-H*?U)}$h }P6H4H
    String ,StringBuffer,StringBuilder的区别(转)
  • 原文地址:https://www.cnblogs.com/carlo/p/9282475.html
Copyright © 2011-2022 走看看