php curl携带 cookie 请求

本文介绍了一个使用 PHP 的 cURL 函数发起 HTTP POST 请求的例子。通过设置不同的参数,如 User-Agent、Cookie 和 IP 地址等,展示了如何向指定 URL 发送数据并接收响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?php
header('Content-Type:text/html;charset=utf-8');
echo  "<pre>";

function curl($cookie,$user_agent,$destURL, $paramStr='',$flag='get',$ip='10.57.22.151',$fromurl='http://www.baidu.com'){
	$curl = curl_init();
	if($flag=='post'){//post传递
		curl_setopt($curl, CURLOPT_POST, 1);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $paramStr);
	}
	curl_setopt($curl, CURLOPT_URL, $destURL);//地址
	
	curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.$ip, 'CLIENT-IP:'.$ip));  //构造IP
	
	
	curl_setopt($curl, CURLOPT_REFERER, $fromurl);
	curl_setopt($curl, CURLOPT_TIMEOUT, 10);#10s超时时间
	
	curl_setopt ($curl, CURLOPT_USERAGENT, $user_agent);
	//curl_setopt ($curl, CURLOPT_COOKIEJAR, $cookie);
	curl_setopt ($curl, CURLOPT_COOKIEFILE, $cookie);
	
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	$str = curl_exec($curl);
	curl_close($curl);
	return $str;
}

	
	$postdata = 'a=11111111111';
	$user_agent = "Mozilla/5.".rand(0,100)." (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0";
	$cookie = 'd:\cookie.txt';
	$getip='66.66.'.rand(0,16).'.'.rand(0,158) ;
	//file_put_contents('d:\cookie.txt', '');
	$cookie_file = dirname(__FILE__).'/cookie.txt';
	ob_start();
	$html =  curl($cookie_file,$user_agent,'http://127.0.0.50/b.php',$postdata,'post',$getip,'');
	ob_end_clean();
	
	echo $html;
	
	

cookie 文件 

127.0.0.50	FALSE	/	FALSE	1418875953	aaaa	cccc



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值