PHP 模拟登录 cookie

前提是你已经获取到登录cookie

$cookie_file = __DIR__ . "/".'cookies.txt';
$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,
    //这部分可以直接复制浏览器 header
    CURLOPT_HTTPHEADER => array(
		'Host: www.abc.com',
		'Accept: application/json, text/javascript, */*; q=0.01',
		'Origin: http://www.abc.com',
		'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
		'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
		'Referer: http://www.abc'),
));
//url
curl_setopt($curl, CURLOPT_URL, $url);
//post fields  参数
curl_setopt($curl, CURLOPT_POSTFIELDS, 'game_code=123');

$response = curl_exec($curl);
$err = curl_error($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

cookies.txt

www.abc.com	FALSE	/	FALSE	0	PHPSESSID	cev06s8h42ka1jc1cjmd8dvpv0
www.abc.com	FALSE	/	FALSE	0	GameCode	1234

猜你喜欢

转载自blog.csdn.net/abc564643122/article/details/83008537
今日推荐