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