PHP Demo login and get Data

Source: Internet
Author: User
Tags preg set cookie

Curl is a powerful PHP library that uses PHP's curl library to easily and efficiently crawl Web pages and capture content, set a cookie to complete a mock landing page, and Curl provides a wealth of functions for developers to get more information about curl from the PHP manual. This article takes the example of open source China (Oschina), and shares the use of curl with everyone.

PHP's curl () is relatively high in terms of crawling web pages, and supports multiple threads, while file_get_contents () is less efficient and, of course, you need to turn on the curl extension when using curl.

Code Combat

First look at the code for the login section:

 
Analog Login
function Login_post ($Url$Cookies,$Post) {
$Curl = Curl_init ();Initializing the Curl module
curl_setopt ($Curl, Curlopt_url,$URL);Address of the login submission
curl_setopt ($Curl, Curlopt_header,0);Whether to display header information
curl_setopt ($Curl, Curlopt_returntransfer,0);//whether the returned information is automatically displayed
curl_setopt ($ Curl, Curlopt_cookiejar, $cookie); //setting cookie information is saved in the specified file
curl_setopt ($ curl, Curlopt_post, 1); //post Way to submit
curl_setopt ($curl, Curlopt_postfields, Http_build_query ($post)); //the information to be submitted
Curl_exec ($curl); Curl_close ($curl); }

The function login_post () First initializes the Curl_init () and then uses curl_setopt () to set the relevant option information, including the URL address to be submitted, the cookie file to be saved, the post data (information such as user name and password), whether to return information, etc. Then curl_exec executes Curl and finally curl_close () frees the resource. Note that PHP's own http_build_query () can convert an array into a concatenated string.

Next, if the login is successful, we want to get the page information after the login is successful.

 
Get data after successful login
function Get_content ($Url$Cookie) {
$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_cookiefile, $cookie); //read the cookie
$rs = curl_exec ( Span class= "Php__keyword" >$ch); //Perform Curl Crawl page content
Curl_close ($ch );
return $RS;
}
/span>

The function get_content () also initializes the curl, then sets the relevant options, performs curl, and frees the resource. Where we set Curlopt_returntransfer to 1 automatically returns information, while Curlopt_cookiefile can read the cookie information saved at login, and finally return the content of the page.

Our ultimate goal is to obtain information after the simulated login, which is useful information that can only be obtained after the normal login is successful. Let's take a look at the mobile version of Open source China for example, and see how to crawl the information after successful login.

 
Set the data for the post
$Post =Array (
' Email ' =' Oschina account ',
' PWD ' =' Oschina password ',
' Goto_page ' ='/my ',
' Error_page ' ='/login ',
' Save_login ' =' 1 ',
' Submit ' =' Login Now '
);

Login Address
$URL ="Http://m.oschina.net/action/user/login";
Set Cookie Save path
$Cookie = dirname (__FILE__).'/cookie_oschina.txt ';
Address to get information after login
$URL2 ="Http://m.oschina.net/my";
Analog Login
Login_post ($Url$Cookies,$Post);
Get information about the login page
$Content = Get_content ($URL2,$Cookies);
//Delete cookie file
@ unlink ($cookie) ;
//match page information
$preg = "/<td class= ' Portrait ' > (. *) <\/td>/i";
Preg_match_all ($preg, $< Span class= "php__variable" >content, $arr);
$str = $arr[1][0";
//output
echo $STR;
/span>

After running the above code, we will see a picture of the avatar that eventually gets to the logged-in user.

Disclaimer: This article is original article, helloweba.com and author have copyright, if need to reprint, please indicate from helloweba.com and keep the original link, otherwise as infringement.

PHP Demo login and get Data

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.