PHP obtains the Cookie to simulate CURL Login

Source: Internet
Author: User

To extract some data from google's search, we found that google was very blocked from the software's data capture. Previously, we forged the USER-AGENT to capture data, but now it is not working. Using packet capture data, we found that Google judges cookies. If you do not have any cookies, you will directly return a 302 jump, and dozens of 302 jumps in a row will not be able to grasp the data.

Therefore, when sending a search command, you need to extract and save the cookies, and then use the saved cookies to send the search command again to capture data normally. This is actually the same as the simulated login on the Forum. First, POST the login, obtain the cookies and save them, and then use the cookies to access them.

 


1. Define the Cookie storage path

Absolute path is required

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



Ii. obtain cookies

Store cookies into files

$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 a browser to obtain the verification code

The server verification code has a vulnerability and can be specified by yourself.

Extract the cookie and submit it to the server so that the server can open the login page as a browser.

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




Iv. 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. Get data on the specified page

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

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.