PHPCurl for login collection

Source: Internet
Author: User
Logon collection means that some Web pages can be viewed only after being logged in using an account. the traditional file_get_contents cannot obtain the content that can be viewed only after logging in. Curl is a powerful component in PHP. it can be used to access data in the header, GET, and POST modes of the HTTP protocol. Users can log on to the site through POST, and then GET the SESSION to obtain the specific page. Note: 1. webpage encoding problems. if the recipient's webpage code is collected from your login, it means that some webpage content can be viewed only after you log on with an account, traditional file_get_contents cannot obtain the content that can be viewed only after login.
Curl is a powerful component in PHP. it can be used to access data in the header, GET, and POST modes of the HTTP protocol. Users can log on to the site through POST, and then GET the SESSION to obtain the specific page.

Note:
1. webpage encoding problems. if the webpage encoding of the other party is inconsistent with yours, use iconv or mb_string for encoding conversion.
2. the COOKIE storage path must be an absolute path. at the beginning of the test, the COOKIE cannot be saved on WINDOWS. Please confirm your path.

Let's talk about the code:

 'Ldstar', 'password' => '100. '); $ cookiepath = $ _ SERVER ["DOCUMENT_ROOT"]. '\\'. MD5 ($ UserURL); // Set the MD5 value of the domain name to the COOKIE file name $ html = curl_post_contents ($ UserURL, $ UserData, $ cookiepath ); // simulate login if ($ html) {if (stripos ($ html, 'login successfully') {$ html = curl_get_contents ($ testURL, True, $ cookiepath ); // get real content} else {$ html = 'login failed';} echo $ html; function curl_get_contents ($ url, $ usecookie = 0, $ cookiepath = '') {$ userAgent = 'kill A/4.0 + (compatible; + MSIE + 6.0; + Windows + NT + 5.1; + SV1) '; $ referer = $ url; $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); // Set the Accessed url curl_setopt ($ ch, CURLOPT_TIMEOUT, 10); // Set the timeout curl_setopt ($ ch, CURLOPT_USERAGENT, $ userAgent); // user access proxy User-Agentcurl_setopt ($ ch, CURLOPT_REFERER, $ referer); // Set refererif ($ usecookie) {curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookiepath ); // COOKIE storage path, used for transfer} curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); // trace 301curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // return result $ r = curl_exec ($ ch); curl_close ($ ch ); return $ r;} function curl_post_contents ($ url, $ data = array (), $ cookiepath = '') {$ userAgent = 'mozilla/4.0 + (compatible; + MSIE + 6.0; + Windows + NT + 5.1; + SV1) '; $ referer = $ url; if (! Is_array ($ data) |! $ Url) return ''; foreach ($ data as $ key => $ value) {$ post. = urlencode ($ key ). '= '. $ value. '&';} rtrim ($ post, '&'); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url ); // Set the url for access curl_setopt ($ ch, CURLOPT_TIMEOUT, 10); // Set the timeout curl_setopt ($ ch, CURLOPT_USERAGENT, $ userAgent ); // user access proxy User-Agentcurl_setopt ($ ch, CURLOPT_REFERER, $ referer); // Set referercurl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 0); // trace 301curl_setopt ($ Ch, CURLOPT_POST, 1); // specify the post data curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post); // add the variable curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookiepath ); // COOKIE storage path. the returned COOKIE storage path curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // The Returned result $ r = curl_exec ($ ch ); curl_close ($ ch); return $ r ;}?>

Original article: http://www.yanghengfei.com/archives/506/

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.