Direct access to the header in the cookie (SessionID) to continue the request
$url = ' test.com '; URL address $post = "id=user&pwd=123456"; Post data//or get$ch = Curl_init ($url); Initialize curl_setopt ($ch, curlopt_header,1); The information of the header file is output as a data stream curl_setopt ($ch, curlopt_returntransfer,1); Returns the obtained output text stream curl_setopt ($ch, Curlopt_postfields, $post); Send post Data $content = Curl_exec ($ch); Perform curl and assign value to $contentpreg_match ('/set-cookie: (. *);/iu ', $content, $STR); Regular match $cookie = $str [1]; Obtaining a cookie (SESSIONID) curl_close ($ch); Close curl/*-----Use cookies-----*/curl_setopt ($ch, Curlopt_cookie, $cookie);//Set Cookie request again
Save to file to request
$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);//After request $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);