PHP: how to generate a Cookie file when reading CURL to simulate logon. _ PHP Tutorial

Source: Internet
Author: User
PHP reads CURL to simulate the method of generating Cookie files during login ,. PHP reads CURL to generate a Cookie file during simulated login. This document describes how PHP reads a Cookie file during simulated login. Share it with you for your reference. Specifically, PHP reads CURL to generate Cookie files during simulated login,

This example describes how PHP reads CURL to generate a Cookie file during simulated login. Share it with you for your reference. The specific implementation method is as follows:

When using the CURL in PHP to simulate logon, a Cookie file will be saved, for example, the following code

The code is as follows:

$ Login_url = 'XXX ';

$ Post_fields ['email '] = 'xxxx ';
$ Post_fields ['password'] = 'xxxx ';
$ Post_fields ['url url'] = 'XXX ';
$ Post_fields ['domain '] = 'XXX. com ';
// Store the cookie file in the temp folder in the root directory of the website.
$ Cookie_file = tempnam ('./temp', 'cooker ');

$ 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 );

// Carry the cookie file to access the page to be accessed
$ 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 );

// Clear cookie files
Unlink ($ cookie_file );

// Output webpage content
Print_r ($ contents );

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

To format the file using php, use the following code:

The code is as follows:

<? Php
$ 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 .'
';
?>

As shown in figure, it has been written to the table.

As a result, you can modify the field if you only want to read it.

I hope this article will help you with PHP programming.




Example: PHP reads CURL to generate a Cookie file during simulated login. Share it with you for your reference. Specific implementation...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.