Home  >  Article  >  Backend Development  >  PHP gets Cookie to simulate login CURL_PHP tutorial

PHP gets Cookie to simulate login CURL_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:12:44831browse

To extract part of the data from Google search, I found that Google blocks the software from capturing its data. In the past, I could forge USER-AGENT to capture the data, but now it doesn’t work. Using packet capture data, we found that Google has determined cookies. When you do not have cookies, it will directly return 302 jumps, and there are dozens of 302 jumps in a row, and no data can be captured at all.

PHP gets Cookie to simulate login CURL_PHP tutorial Therefore, when sending a search command, you need to extract and save the cookies first, and then use the saved cookie to send the search command again to capture the data normally. This is actually the same as the simulated login of the forum. You need to POST to log in first, get the cookies and save them, and then use the cookies to access.


1. Define Cookie storage path

Must use absolute path

$cookie_jar = dirname(__FILE__)."/pic.cookie";



2. Obtain Cookie

Save cookies to file

$url = "http://1.2.3.4/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
$content = curl_exec($ch);
curl_close($ch);




3. Simulate the browser to obtain the verification code

There is a loophole in the server verification code, you can specify it yourself

Take out the cookie and submit it to the server together, so that the server thinks it is the browser opening the login page

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://1.2.3.4/getCheckpic.action?rand=6836.185874812305');
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($ch);
curl_close($ch);




4. POST submission

$post = "name=2&userType=1&passwd=asdf&loginType=1&rand=6836&imageField.x=25&imageField.y=7";    
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://1.2.3.4/loginstudent.action");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
$result=curl_exec($ch);
curl_close($ch);




5. Go to the designated page to obtain data

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://1.2.3.4/accountcardUser.action");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,0);        
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
$html=curl_exec($ch);
// var_dump($html);
curl_close($ch);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444553.htmlTechArticleTo extract some data from Google search, I found that Google is very powerful in blocking the data captured by the software. I used to fake it. USER-AGENT can capture data, but now it cannot. Use the catch...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn