Home > Article > Backend Development > PHP curl simulated login_PHP tutorial
//Submit data, generate cookies, and save cookies in the temporary directory
//Create a file with a unique file name in the specified directory. If the directory does not exist, tempnam() will generate a file in the system temporary directory and return its file name
$cookie_file=tempnam('./temp','cookie');
$ch=curl_init();
$login_url="http://www.xxx.com/login/";
$curlPost="username=username&password=password";
curl_setopt($ch,CURLOPT_URL,$login_url);
//When enabled, the information of the header file will be output as a data stream
curl_setopt( $ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//Display http information output
curl_setopt($ch,CURLOPT_POST,1);//POST request
curl_setopt($ ch,CURLOPT_POSTFIELDS,$curlPost);//Request body
//Set the file to save cookie information after the connection is completed
curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file);
curl_exec($ch);
curl_close($ch);