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);
}

 

posted @ 2018-07-09 10:16  carlo-z  阅读(3415)  评论(0编辑  收藏  举报