curl does not use file access cookie php uses curl to get the cookie example

  • 2020-12-21 17:59:51
  • OfStack


/*----- save COOKIE-----*/
$url = 'www.xxx.com'; //url address 
$post = "id=user&pwd=123456"; //POST data 
$ch = curl_init($url); // Initialize the 
curl_setopt($ch,CURLOPT_HEADER,1); // Output the information from the header file as a data stream 
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); // Returns the output text stream obtained 
curl_setopt($ch,CURLOPT_POSTFIELDS,$post); // send POST data 
$content = curl_exec($ch); // perform curl And assigned to it $content
preg_match('/Set-Cookie:(.*);/iU',$content,$str); // Regular match 
$cookie = $str[1]; // To obtain COOKIE ( SESSIONID ) 
curl_close($ch); // Shut down curl
/*----- use COOKIE-----*/
curl_setopt($ch,CURLOPT_COOKIE,$cookie);


Related articles: