Phpcurl is used to log on to and obtain content.

Source: Internet
Author: User
Phpcurl login and retrieval content class. how can you use it? php automatic login simulated login to obtain source code

Business requirements:
You can use a php script to automatically log on to the webpage and obtain the content on the webpage after logon.

I found the following program on the Internet, but I found it failed during the verification process. Please check it out. thank you.

Class CURL
{
Var $ cookie_file; // Set the Cookie file storage path and file name
Var $ loginurl; // address of the logon location
Var $ actionstr; // login parameter
Function _ construct ()
{
$ This-> cookie_file = dirname (_ FILE _). "/cookie _". md5 (basename (_ FILE _). ". txt ";
If (! File_exists ($ this-> cookie_file ))
{// Check whether the Cookie exists
$ Str = $ this-> vget ('jroam'); // obtain random logon values
Preg_match ("/name = \" formhash \ "value = \"(.*?) \ "/Is", $ str, $ hash); // extract random logon values
$ This-> vlogin ($ this-> loginurl, $ this-> actionstr); // log on to get the Cookie
}
}

Function vlogin ($ url, $ data) {// simulates logon to obtain the Cookie function
$ Curl = curl_init (); // Start a CURL session
Curl_setopt ($ curl, CURLOPT_URL, $ url); // address to be accessed
Curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, 0); // Check the certificate source
Curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1); // check whether the SSL encryption algorithm exists from the certificate
Curl_setopt ($ curl, CURLOPT_USERAGENT, $ _ SERVER ['http _ USER_AGENT ']); // simulate the browser used by the user
Curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1); // use automatic redirect
Curl_setopt ($ curl, CURLOPT_AUTOREFERER, 1); // automatically sets Referer
Curl_setopt ($ curl, CURLOPT_POST, 1); // send a regular Post request
Curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data); // data packet submitted by Post
Curl_setopt ($ curl, CURLOPT_COOKIEJAR, $ this-> cookie_file); // name of the file that stores Cookie information
Curl_setopt ($ curl, CURLOPT_COOKIEFILE, $ this-> cookie_file); // read the Cookie information stored above
Curl_setopt ($ curl, CURLOPT_TIMEOUT, 30); // sets the timeout limit to prevent endless loops.
Curl_setopt ($ curl, CURLOPT_HEADER, 0); // display the returned Header content.
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); // The obtained information is returned as a file stream.
$ TmpInfo = curl_exec ($ curl); // perform the operation
If (curl_errno ($ curl )){
Echo 'errno'. curl_error ($ curl );
}
Curl_close ($ curl); // Close the CURL session
Return $ tmpInfo; // return data
}

Function vget ($ url) {// simulates the function to obtain the content
$ Curl = curl_init (); // Start a CURL session
Curl_setopt ($ curl, CURLOPT_URL, $ url); // address to be accessed
Curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, 0); // Check the certificate source
Curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1); // check whether the SSL encryption algorithm exists from the certificate
Curl_setopt ($ curl, CURLOPT_USERAGENT, $ _ SERVER ['http _ USER_AGENT ']); // simulate the browser used by the user
Curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1); // use automatic redirect
Curl_setopt ($ curl, CURLOPT_AUTOREFERER, 1); // automatically sets Referer
Curl_setopt ($ curl, CURLOPT_HTTPGET, 1); // send a regular Post request
Curl_setopt ($ curl, CURLOPT_COOKIEFILE, $ this-> cookie_file); // read the Cookie information stored above
Curl_setopt ($ curl, CURLOPT_TIMEOUT, 30); // sets the timeout limit to prevent endless loops.
Curl_setopt ($ curl, CURLOPT_HEADER, 0); // display the returned Header content.
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); // The obtained information is returned as a file stream.
$ TmpInfo = curl_exec ($ curl); // perform the operation
If (curl_errno ($ curl )){
Echo 'errno'. curl_error ($ curl );
}
Curl_close ($ curl); // Close the CURL session
Return $ tmpInfo; // return data
}

Function vpost ($ url, $ data) {// simulate the data submission function
$ Curl = curl_init (); // Start a CURL session
Curl_setopt ($ curl, CURLOPT_URL, $ url); // address to be accessed
Curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, 0); // Check the certificate source
Curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1); // check whether the SSL encryption algorithm exists from the certificate
Curl_setopt ($ curl, CURLOPT_USERAGENT, $ _ SERVER ['http _ USER_AGENT ']); // simulate the browser used by the user
Curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1); // use automatic redirect
Curl_setopt ($ curl, CURLOPT_AUTOREFERER, 1); // automatically sets Referer
Curl_setopt ($ curl, CURLOPT_POST, 1); // send a regular Post request
Curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data); // data packet submitted by Post
Curl_setopt ($ curl, CURLOPT_COOKIEFILE, $ this-> cookie_file); // read the Cookie information stored above
Curl_setopt ($ curl, CURLOPT_TIMEOUT, 30); // sets the timeout limit to prevent endless loops.
Curl_setopt ($ curl, CURLOPT_HEADER, 0); // display the returned Header content.
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); // The obtained information is returned as a file stream.
$ TmpInfo = curl_exec ($ curl); // perform the operation
If (curl_errno ($ curl )){
Echo 'errno'. curl_error ($ curl );
}
Curl_close ($ curl); // key CURL session
Return $ tmpInfo; // return data
}
Function delcookie ($ cookie_file) {// delete a Cookie function
@ Unlink ($ cookie_file); // delete
}
}

// Application instance
$ Mycurl = new CURL ();
$ Mycurl-> loginurl = "http: // 192.168.0.2/login. php"; // logon address
$ Mycurl-> actionstr = "user_name = 111 & uspassword = 1234"; // parameter string;

$ Url1 = 'http: // 192.168.0.2/userlist. php ";
$ Htmlcontent = $ mycurl-> vget ($ url1); // output source code of the target address
Echo $ htmlcontent;
?>


Reply to discussion (solution)

In addition, it is in the linux platform environment.

Make sure you have passed the correct parameters

$ Url1 = 'http: // 192.168.0.2/userlist. php ";

Errnococould not resolve host: jroam; [color = # FF0000] Host not foundErrno malformedErrnocouldn't connect to host [/color]

I have already solved the problem. thank you for your consideration.

How can this problem be solved?

I will go. if you solve this problem, you will not lose a solution! Despise You!

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.