首页 > php > php curl函数应用方法之模拟浏览器2

php curl函数应用方法之模拟浏览器2

post例子

$url = "http://www.mytest.com/curl/login.php";  //请求的url地址
$user = "zkg111"; //用户名
$pass = "123456"; 
$postdata = "user_name=".$user."&password=".$pass;  //请求的数据,以 & 符号分割
$curl = curl_init(); //开启curl
curl_setopt($curl, CURLOPT_URL, $url); //设置请求地址
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  //是否输出 1 or true 是不输出 0  or false输出
curl_setopt($curl, CURLOPT_POST, 1); //是否使用post方法请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);  //post数据


echo $data = curl_exec($curl); //执行curl操作
curl_close($curl);

获取cookie模拟登陆

$url = "http://bbs.lampbrother.net/login.php";
$urls = "http://bbs.lampbrother.net";
$lgt = 0;
$user = "XXXX";
$pass = "XXXX";
$question = 0;
$hideid = 1;
 $cookie_file    =    tempnam('./temp','cookie');
 $postdata = "forward=&jumpurl=".$urls."&step=2&lgt=".$lgt."&pwuser=".$user."&pwpwd=".$pass."&question=".$question."&answer=&hideid=".$hideid;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
 $data = curl_exec($ch);
curl_close($ch);
 //echo $data;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://bbs.lampbrother.net/');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
curl_exec($ch);
curl_close($ch);

上一篇: php curl函数应用方法之模拟浏览器1

下一篇: php curl函数应用方法之模拟浏览器3