• 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);
    }
  • 相关阅读:
    生成4位随机验证码工具类
    MD5加随机盐工具类
    Excel文件解析工具类
    实践作业4:Web测试实践(小组作业)记录4
    实践作业4:Web测试实践(小组作业)记录3
    实践作业4:Web测试实践(小组作业)记录2
    实践作业4:Web测试实践(小组作业)记录1
    实践作业3:白盒测试实践(小组作业)记录3
    实践作业3:白盒测试实践(小组作业)记录2
    实践作业3:白盒测试实践(小组作业)记录1
  • 原文地址:https://www.cnblogs.com/carlo/p/9282475.html
Copyright © 2020-2023  润新知