[PHP Automation-Advanced]001. Simulate login and collect data

Source: Internet
Author: User
Tags http post

Introduction: PHP can be implemented through Libcurl to achieve the simulation login, submit data, lawlessness, burn kill and other matters.

Simply explain "libcurl" and fill your head:

    • Libcurl currently supports HTTP, HTTPS, FTP, Gopher, Telnet, dict, file, and LDAP protocols.
    • Libcurl also supports HTTPS authentication, HTTP POST, HTTP PUT, FTP upload (This can also be done via PHP's FTP extension), HTTP forms-based uploads, proxies, cookies, and Username + password Authentication.

Using curl, you can implement get and post request methods, forge the routing ip, and so on ...

Cut into the topic

In this talk, we have prepared for you how to use Libcurl to " impersonate the user and collect data ."

Opening instructions

By default, PHP's Curl function is not turned on, so you have to start this feature yourself, you need to do the following:

In the php.ini , extension= Php_curl.dll in front of the " ; " Number!!!

Straightforward

Directly on the code, code comments are already very detailed! Ladies and gentlemen, crossing, please see below:

Step 1:

Creates a temporary file with a unique file name that is used for storing cookie information. (If successful, a new temporary file name is returned.) If it fails, it returns false. )

Specify a cookie to save the file
$cookie _fileTempnam

Step 2:

Initializes a Curl object and simultaneously specifies the URL to be requested, requesting the data to be carried.

// Request URL $url  = "Http://www.phpyu.com/index.php?action=login";
// Request URL
$data = "user=xx&pw=xx";; // initializing a Curl object $ch = curl_init ($url);

Step 3:

Sets the Curl object parameter.

//Set Request header [None] (description: If you need to include your head in the output, set the property to a value other than 0.) )curl_setopt ($ch, Curlopt_header, 0);//setting does not output to the browsercurl_setopt ($ch, Curlopt_returntransfer, 1);//set the request to Universal POST (note: If you want PHP to do a regular HTTP POST, set the property to a value other than 0.) This post is an ordinary application/x-www-from-urlencoded type, most of which is used by HTML forms. )
curl_setopt ($ch, curlopt_post, 1); Set request to carry datacurl_setopt ($ch, Curlopt_postfields,$data);//set the cookie to save the file (Note: Save the returned cookie information in the $cookie_file file)curl_setopt ($ch, Curlopt_cookiejar,$cookie _file);

Step 4:

Executes the request and closes it.

// Execution Request curl_exec ($ch); // Close Object curl_close ($ch);

The above has completed the process of simulating login .

The complete code is as follows:

[PHP Automation-Advanced]001. Simulate login and collect data

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.