Home > Backend Development > PHP Tutorial > PHP reads CURL and generates cookie files when simulating login, _PHP tutorial

PHP reads CURL and generates cookie files when simulating login, _PHP tutorial

WBOY
Release: 2016-07-13 10:15:14
Original
930 people have browsed it

How PHP reads CURL to generate a cookie file when simulating login,

The example in this article describes how PHP reads CURL to generate a cookie file when simulating login. Share it with everyone for your reference. The specific implementation method is as follows:

When using CURL in PHP to simulate login, a Cookie file will be saved, such as the following code

Copy code The code is as follows:
$login_url = 'XXX';

$post_fields['email'] = 'XXXX';
$post_fields['password'] = 'XXXX';
$post_fields['origURL'] = 'XXX';
$post_fields['domain'] = 'xxx.com';
//The cookie file is stored in the temp folder in the root directory of the website
$cookie_file = tempnam('./temp','cookie');

$ch = curl_init($login_url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_exec($ch);
curl_close($ch);

//Bring the cookie file and access the page you need to visit
$send_url='xxx.com';
$ch = curl_init($send_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
$contents = curl_exec($ch);
curl_close($ch);

//Clean cookie files
unlink($cookie_file);

//Output web page content
print_r($contents);

Save a temporary file with cookie prefix in the temp folder, for example: coo3A98.tmp file
Open this file and get the following code:

To use php to format the file, use the following code to achieve it

Copy code The code is as follows:
$cookie_folder = dirname(__FILE__)."/temp";
$lines = file($cookie_folder.'/coo3A98.tmp');

$trows = '';

foreach($lines as $line) {
If($line[0] != '#' && substr_count($line, "t") == 6) {
         $tokens = explode("t", $line);
         $tokens = array_map('trim', $tokens);
         $tokens[4] = date('Y-m-d h:i:s', $tokens[4]);
$trows .= '' . implode('', $tokens) . '' . PHP_EOL;
}  
}
echo ''.PHP_EOL.''.PHP_EOL.$trows.''.PHP_EOL.'
';
?>

After running, as shown in the figure below, it has been written to the table

You’re done. If you only read the fields, you can modify it yourself.

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/906109.htmlTechArticleHow to generate a Cookie file when PHP reads CURL to simulate login. This article describes how PHP reads CURL to simulate login. Method to generate cookie files. Share it with everyone for your reference. Specific implementation...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template