Php CURL allows you to simulate logon and collect data

Source: Internet
Author: User
Tags vars

In php, we use a simple collection method (for example, file_get_contents). However, if we want to simulate a login user and collect and use it, we cannot, we can use the CURL function to simulate logon and collect data.

Here is a bit. By default, PHP's CURL function is not enabled, so you need to enable this function on your own. remove the ";" before extension = php_curl.dll in ini !!!
Well, let me talk about the program last night. Although it was not successful at last, I still learned something.
 

The Code is as follows: Copy code

$ Login = "http://www.phpyu.com/index.php? Action = login ";
$ Post_file = "user = ×× & pw = ×× ";
$ Cookie_file = tempnam ('./temp', 'cooker ');

///// Create a temporary file with a unique file name. If yes, the function returns a new temporary file name. If it fails, false is returned.

The Code is as follows: Copy code

$ Ch = curl_init ($ login_url); //// initialize a CURL object
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );

// If you want to include a header in the output, set this option to a non-zero value.

The Code is as follows: Copy code

Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // The setting is not output on the browser.
Curl_setopt ($ ch, CURLOPT_POST, 1 );

///// If you want PHP to create a regular http post, set this option to a non-zero value. This POST is a common application/x-www-from-urlencoded type, most of which are used by HTML forms.

The Code is as follows: Copy code

Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_file); // transmits a string of all data for the HTTP "POST" operation.
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_file); // Save the returned cookie information in the $ cookie_jar file.
Curl_exec ($ ch); // Execute
Curl_close ($ ch); // close


The above process has been completed to simulate Logon
 

The following is to enter the page with permissions. Remember that you have logged on now. You should save the cookie of the logon credential.

The Code is as follows: Copy code

$ Url = "http://www.phpyu.com/admin/×× ";
$ Ch = curl_init ($ url );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 0 );

//// Display this page in a browser. Pay special attention to this page !! If you display it in a browser, the $ contents below will become a boolean type true.

The Code is as follows: Copy code

Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie_file );
$ Contents = curl_exec ($ ch );

Example 2

The Code is as follows: Copy code

<? Php
$ Cookie_path = './'; // you can specify the cookie storage path.


// ----- Log on to the form data to be submitted ---------------
$ Vars ['username'] = 'zhang san ';
$ Vars ['pwd'] = '20140901 ';
//-------------------------------------
$ Method_post = true;
// The submitted url (the absolute address of the action in the form)
$ Url = 'HTTP: // *****. com/login ';
//----------------------------


$ Ch = curl_init ();
$ Params [CURLOPT_URL] = $ url; // request url
$ Params [CURLOPT_HEADER] = true; // whether to return response header information
$ Params [CURLOPT_RETURNTRANSFER] = true; // whether to return the result
$ Params [CURLOPT_FOLLOWLOCATION] = true; // whether to redirect
$ Params [CURLOPT_USERAGENT] = 'mozilla/5.0 (Windows NT 5.1; rv: 9.0.1) Gecko/20100101 Firefox/9.0.1 ';

$ Postfields = '';
Foreach ($ vars as $ key => $ value ){
$ Postfields. = urlencode ($ key). '='. urlencode ($ value ).'&';
}

$ Params [CURLOPT_POST] = true;
$ Params [CURLOPT_POSTFIELDS] = $ postfields;

// Determine whether a cookie exists. If yes, use it directly.
If (isset ($ _ COOKIE ['cookie _ jar']) & ($ _ COOKIE ['cookie _ jar'] | is_file ($ _ cookie ['cookie _ jar'])
{
$ Params [CURLOPT_COOKIEFILE] = $ _ COOKIE ['cookie _ jar']; // determine the cookie here
}
Else
{
$ Cookie_jar = tempnam ($ cookie_path, 'cooke'); // generate a cookie file
$ Params [CURLOPT_COOKIEJAR] = $ cookie_jar; // write cookie Information
Setcookie ('cookie _ jar', $ cookie_jar); // Save the cookie Path
}
Curl_setopt_array ($ ch, $ params); // you can specify the curl parameter.
$ Content = curl_exec ($ ch); // Execute


Echo'
'; Echo $ content; // output the logon result
/*
// --------- Request another address again after successful logon. If there are multiple addresses, You can execute it cyclically ---------
Echo'
--------------------------------------------------------------------------------
';
$ Nexturl = 'HTTP: // *****. com/test ';
$ Params [CURLOPT_URL] = $ nexturl;
$ Params [CURLOPT_POSTFIELDS] = '';
Curl_setopt_array ($ ch, $ params); // you can specify the curl parameter.
$ Content = curl_exec ($ ch); // Execute
Echo $ content; // output request results
//-------------------------------------------------
*/
Curl_close ($ ch); // close the connection

?>

Note: if you cannot request an https site, it may be because the certificate or domain name cannot be verified. You only need to add the following two items before curl_setopt_array:

The Code is as follows: Copy code

$ Params [CURLOPT_SSL_VERIFYPEER] = false;

$ Params [CURLOPT_SSL_VERIFYHOST] = false;


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.