时间:2021-07-01 10:21:17 帮助过:50人阅读
当然了,手册上明白写的curl的cookie是3个,但是嘛,不是还有个header的参数嘛,里面可以包含cookie.
curl非常的好用。最主要的是要熟悉curl_setopt的用法。
curl_setopt ($ch, CURLOPT_COOKIE , $cookie );
这里的cookie值要用;分开的,而不是用&。也不需要用urlencode编码,当然了,编码了就更好。
$cookie = "a=b;c=d;name=方世玉";
注意使用这个的时候,不可在 curl_setopt ($ch, CURLOPT_HTTPHEADER , $header );
的$header里包含Cookie参数,否则会重叠,造成cookie不可预见的情况发生。
php $url = "http://www.test.com/zzzz.php"; $post_data = array ( "foo" =< "bar", "query" =< "Nettuts", "action" =< "Submit" ); $cookie_jar_index = 'f:/js/test/cookie.txt'; $cookie = "a=b;c=d;name=方世玉"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 我们在POST数据哦! curl_setopt($ch, CURLOPT_POST, 1); // 把post的变量加上 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_COOKIE, $cookie); $output = curl_exec($ch); curl_close($ch); echo $output;
附上用来分析IE中cookie的代码
php function join_cookie($cook) { foreach( $cook as $k=<$v ) { $d[] =$k."=".$v; } $data = implode(";",$d); return $data; } function pase_cookie($cookFile,$encode=true) { $cookie = file_get_contents ( $cookFile ); $citem = explode("*\n",$cookie); foreach( $citem as $c ) { list($ckey,$cvalue) = explode("\n",$c); if($ckey!='')$cook[$ckey] = $cvalue; } return $cook; }