php模拟浏览器进行带cookie的post数据

php模拟浏览器进行带cookie的post数据

 

<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
$post_data = array();
$post_data[‘user’] = “zs”;
$post_data[‘passwd’] = “zspwd”;
$post_data[‘submit’] = “submit”;
$url=’http://localhost/test/index.php’;
$cookie = “a=b;c=d”;
$o=””;
foreach ($post_data as $k=>$v)
{
$o.= “$k=”.urlencode($v).”&”;
}
$post_data=substr($o,0,-1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt ($ch, CURLOPT_COOKIE , $cookie );
//为了支持cookie
curl_setopt($ch, CURLOPT_COOKIEJAR, ‘cookie.txt’);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$result = curl_exec($ch);
echo $result;
?>

Leave a Reply

Your email address will not be published. Required fields are marked *