PHP Curl Simulation Login forum and collect data instance _php tutorial

Source: Internet
Author: User
This article gives you a brief introduction to the PHP Curl Simulation Landing Forum and collect data instances, if you are interested in using the Curl Simulation login function to enter the reference.

To emulate a browser's access to a website, it is preferred to learn to observe how the browser sends HTTP messages and what the Web server returns to the browser. I recommend the installation of a foreign developer of the HttpWatch software, it is best to make a cracked version, or some features are not used. After the installation of this software is embedded in IE, the start record, enter the address bar after entering the URL, it will be the browser and the server to scan all the communication, let you in a sweeping. The use of this software is not introduced in this document.

Simulation browser landing application development, the most critical place is the breakthrough login verification. Curl Technology not only supports HTTP, it also supports HTTPS. The difference is a layer of SSL-encrypted transmission. If you are logging on to an HTTPS website, PHP remembers to support OpenSSL. Let's take an example to analyze it.

The code is as follows Copy Code

$discuz _url = ' http://127.0.0.1/discuz/'; Forum Address
$login _url = $discuz _url. ' Logging.php?action=login '; Login Page Address

$post _fields = Array ();
The following two items do not need to be modified
$post _fields[' loginfield '] = ' username ';
$post _fields[' loginsubmit ') = ' true ';
User name and password must be filled in
$post _fields[' username '] = ' tianxin ';
$post _fields[' password '] = ' 111111 ';
Safety questions
$post _fields[' QuestionID '] = 0;
$post _fields[' answer '] = ';
@todo Verification Code
$post _fields[' seccodeverify '] = ';

Get Form Formhash
$ch = Curl_init ($login _url);
curl_setopt ($ch, Curlopt_header, 0);
curl_setopt ($ch, Curlopt_returntransfer, 1);
$contents = curl_exec ($ch);
Curl_close ($ch);
Preg_match ('/ /I ', $contents, $matches);
if (!empty ($matches)) {
$formhash = $matches [1];
} else {
Die (' not found the Forumhash. ');
}

Post data, get the Cookie,cookie file in the temp directory on the website
$cookie _file = Tempnam ('./temp ', ' Cookie ');

$ch = Curl_init ($login _url);
curl_setopt ($ch, Curlopt_header, 0);
curl_setopt ($ch, Curlopt_returntransfer, 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);

The key cookie file can be taken with the cookie file to simulate the post, FID for the Forum column ID
$send _url = $discuz _url. "Post.php?action=newthread&fid=2";


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

Here the hash code and the login window hash code of the regular is not the same, here hidden more than an id attribute
Preg_match ('/ /I ', $contents, $matches);
if (!empty ($matches)) {
$formhash = $matches [1];
} else {
Die (' not found the Forumhash. ');
}


$post _data = Array ();
Post title
$post _data[' subject '] = ' test2 ';
Post Content
$post _data[' message '] = ' test2 ';
$post _data[' topicsubmit '] = "yes";
$post _data[' extra '] = ';
Posts tab
$post _data[' tags '] = ' test ';
The hash code of the post, this is very key! If this hash code is missing, Discuz will warn you that the wrong page is being routed.
$post _data[' formhash ') = $formhash;


$ch = Curl_init ($send _url);
curl_setopt ($ch, Curlopt_referer, $send _url); Camouflage Referer
curl_setopt ($ch, Curlopt_header, 0);
curl_setopt ($ch, Curlopt_returntransfer, 0);
curl_setopt ($ch, Curlopt_cookiefile, $cookie _file);
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_postfields, $post _data);
$contents = curl_exec ($ch);
Curl_close ($ch);

Clean up Cookie Files
Unlink ($cookie _file);
?>

Curl for Web site simulation landing

code as follows copy code

http://www.bkjia.com/PHPjc/632770.html www.bkjia.com true http://www.bkjia.com/PHPjc/632770.html techarticle This article gives you a brief introduction to the PHP Curl Simulation Landing Forum and collect data instances, if you are interested in using the Curl Simulation login function to enter the reference. To simulate a browser visit ...

  • 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.