Use PHP curl to simulate a browser to grab web site information

  • 2020-10-23 20:04:20
  • OfStack

The official explanation
curl is a file transfer tool that USES the URL syntax to work from the command line. curl is a file transfer tool that USES the URL syntax to work from the command line.
It supports many protocols: FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE and LDAP. curl also supports HTTPS authentication, HTTP POST method, HTTP PUT method, FTP upload, kerberos authentication, HTTP upload, proxy server, cookies, user name/password authentication, download file breakpoint and continue,
Upload file breakpoint continuation, http proxy server pipeline (proxy tunneling), it even supports IPv6, socks5 proxy server, upload files to FTP server through http proxy server, etc., with 10 powerful functions.

curl on 1

The curl function is applied in PHP
Simply put, there are 4 steps to 1
curl_init();
curl_setopt();
curl_exec();
curl_close();

The most important command is curl_setopt();

1 simple post request example
index.php


<?php
$url = "http://www.mytest.com/curl/login.php";  // The request of url address 
$user = "zkg111"; // The user name 
$pass = "123456"; 
$postdata = "user_name=".$user."&password=".$pass;  // The requested data to  &  Character segmentation 
$curl = curl_init(); // open curl
curl_setopt($curl, CURLOPT_URL, $url); // Set the request address 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  // Whether the output  1 or true  It is not the output  0  or false The output 
curl_setopt($curl, CURLOPT_POST, 1); // Whether to use post Method request 
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);  //post data 

 
echo $data = curl_exec($curl); // perform curl operation 
curl_close($curl);
?>

The following is a simple example, I casually opened the brothers of the forum, and then simulated the 1 brothers of the forum login, if the need to post the principle is one kind of, redirect the page, submit data
Note in particular that cookie's save directory windows7 must be in the./temp directory, I started to create a new folder by myself, found that the save is correct, but cookie read the wrong, so it is still in many places
Ask a question, but did not answer right, toss about for a few days should save the file for./temp directory to be able to remind other friends and I 1 kind of blind turn

<?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','cookie');
 $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);
?>


Related articles: