首頁  >  文章  >  後端開發  >  PHP curl session 使用

PHP curl session 使用

WBOY
WBOY原創
2016-06-23 13:17:192111瀏覽

直接获取header中cookie(sessionid)用来继续请求

$url = 'test.com'; //url地址$post = "id=user&pwd=123456"; //POST数据//或GET$ch = curl_init($url); //初始化curl_setopt($ch,CURLOPT_HEADER,1); //将头文件的信息作为数据流输出curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //返回获取的输出文本流curl_setopt($ch,CURLOPT_POSTFIELDS,$post); //发送POST数据$content = curl_exec($ch); //执行curl并赋值给$contentpreg_match('/Set-Cookie:(.*);/iU',$content,$str); //正则匹配$cookie = $str[1]; //获得COOKIE(SESSIONID)curl_close($ch); //关闭curl/*-----使用COOKIE-----*/curl_setopt($ch,CURLOPT_COOKIE,$cookie);//设置cookie再次请求

保存到文件用来请求

$cookie_jar = dirname(__FILE__)."/pic.cookie";$url = "http://test.com/";$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);$content = curl_exec($ch);curl_close($ch);//之后的请求$ch = curl_init();curl_setopt($ch, CURLOPT_URL, 'http://test.com/check.action?');curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $post);$ret = curl_exec($ch);curl_close($ch);


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn