Use PHP curl to simulate a browser to capture website information

Source: Internet
Author: User

Official explanation
Curl is a File Transfer tool that uses the URL syntax to work in the command line mode. Curl is a File Transfer tool that uses the URL syntax to work in the command line mode.
It supports many protocols: FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE, and LDAP. Curl also supports HTTPS authentication, http post, http put, FTP upload, kerberos authentication, HTTP upload, proxy server, cookies, user name/password authentication, and resumable download of downloaded files,
Resumable upload of uploaded files, the http proxy Server pipeline (proxy tunneling), and even IPv6 and socks5 proxy servers, which upload files to the FTP server through the http proxy server, are very powerful.

Curl 1

Curl Function Application in PHP
Simply put, there are four steps in total.
Curl_init ();
Curl_setopt ();
Curl_exec ();
Curl_close ();

The most important command is curl_setopt ();

A simple post request example
Index. php
Copy codeThe Code is as follows:
<? Php
$ Url = "http://www.mytest.com/curl/login.php"; // The requested url
$ User = "zkg111"; // user Name
$ Pass = "123456 ";
$ Postdata = "user_name =". $ user. "& password =". $ pass; // request data, separated by & Symbol
$ Curl = curl_init (); // enable curl
Curl_setopt ($ curl, CURLOPT_URL, $ url); // you can specify the request address.
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); // whether to output 1 or true is not output 0 or false
Curl_setopt ($ curl, CURLOPT_POST, 1); // whether to use the post request
Curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ postdata); // post Data

 
Echo $ data = curl_exec ($ curl); // perform the curl operation
Curl_close ($ curl );
?>

The following is a simple example. I opened the brothers forum and simulated the login of the brothers Forum. If you need to post a message, the principles are the same. Transfer the page and submit the data.
Note that the cookie storage directory under Windows 7 must be in. in the/temp directory, I first created a new folder and found that the folder is correct, but the cookie is not read correctly.
I asked a question, but I didn't answer the question correctly. After several days, I saved the file as a./temp directory, reminding other friends not to turn around like me.
Copy codeThe Code is as follows:
<? Php
$ Url = "http://bbs.lampbrother.net/login.php ";
$ Urls = "http://bbs.lampbrother.net ";
$ Lgt = 0;
$ User = "XXXX ";
$ Pass = "XXXX ";
$ Question = 0;
$ Hideid = 1;
$ Cookie_file = tempnam ('./temp', 'cooker ');
$ Postdata = "forward = & jumpurl = ". $ urls. "& step = 2 & lgt = ". $ lgt. "& pwuser = ". $ user. "& pwpwd = ". $ pass. "& question = ". $ question. "& answer = & hideid = ". $ hideid;
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ postdata );
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_file );
Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, true );
$ Data = curl_exec ($ ch );
Curl_close ($ ch );
// Echo $ data;
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, 'HTTP: // bbs.lampbrother.net /');
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 0 );
Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie_file );
Curl_exec ($ ch );
Curl_close ($ ch );
?>

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.