復(fù)制代碼 代碼如下:
/*-----保存COOKIE-----*/
$url = 'www.xxx.com'; //url地址
$post = "id=user&pwd=123456"; //POST數(shù)據(jù)
$ch = curl_init($url); //初始化
curl_setopt($ch,CURLOPT_HEADER,1); //將頭文件的信息作為數(shù)據(jù)流輸出
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); //返回獲取的輸出文本流
curl_setopt($ch,CURLOPT_POSTFIELDS,$post); //發(fā)送POST數(shù)據(jù)
$content = curl_exec($ch); //執(zhí)行curl并賦值給$content
preg_match('/Set-Cookie:(.*);/iU',$content,$str); //正則匹配
$cookie = $str[1]; //獲得COOKIE(SESSIONID)
curl_close($ch); //關(guān)閉curl
/*-----使用COOKIE-----*/
curl_setopt($ch,CURLOPT_COOKIE,$cookie);