时间:2021-07-01 10:21:17 帮助过:23人阅读
$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);