首页 >后端开发 >php教程 >PHP curl session 使用

PHP curl session 使用

WBOY
WBOY原创
2016-06-23 13:17:192128浏览

直接获取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