Php simulated login capture page content curl usage

Source: Internet
Author: User
Php Chinese network (www.php.cn) provides the most comprehensive basic tutorial on programming technology, introducing HTML, CSS, Javascript, Python, Java, Ruby, C, PHP, basic knowledge of MySQL and other programming languages. At the same time, this site also provides a large number of online instances, through which you can better learn programming... In normal development, a page content is often captured, but sometimes some pages need to be logged on for access. The most common is the forum. at this time, we need to simulate login using curl. General Idea: first request to extract and save the cookies, and then use the saved cookies to send a request again to obtain the page content. below we will directly go to the code

 'Pythontab', 'password' => 'pythontab',); // curl initialization $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url ); // Set it to post request curl_setopt ($ ch, CURLOPT_POST, true); // Set the returned header information to null curl_setopt ($ ch, CURLOPT_HEADER, 0 ); // post data curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); // The location of the cookie file stored in curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookieFile ); // Set the returned data to be stored as a variable instead of directly outputting curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // execute the request $ ret = curl_exec ($ ch ); // close the connection curl_close ($ ch); // Step 2: Add the cookie request to the login page $ url = 'http: // www.pythontab.com '; // curl initialization $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); // Set it to post request curl_setopt ($ ch, CURLOPT_POST, true ); // Set the returned header information to null curl_setopt ($ ch, CURLOPT_HEADER, 0); // set the location of the cookie information file. Note that the returned header information is different from that obtained in step 2, read curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookieFile); // Set the data to be stored as a variable instead of directly outputting curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true ); // execute the request $ ret = curl_exec ($ ch); // close the connection curl_close ($ ch); // Print the captured content var_dump ($ ret );

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.