Curl simulated logon implementation tutorial _ PHP Tutorial

Source: Internet
Author: User
Curl simulated logon implementation tutorial. The curl simulated logon implementation tutorial simulates logon to the 126 mailbox, Renren, etc. you can modify it as needed. the code below simulates login to the verification code website. First, you can obtain the c curl simulated logon implementation tutorial of the verification code image.

Simulate login to the 126 mailbox, Renren and so on, and modify the code as needed. the code is as follows:

// Simulate login to the verification code website, first obtain the cookie of the verification code Image

Define ('script _ root', dirname (_ FILE __).'/');

$ CookieFile = SCRIPT_ROOT. 'cookie. tmp ';

/* Simulate a browser */

$ User_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;. net clr 1.1.4322 )";

// If you know the cookie, you can directly send it to the target browser.

// $ Cookie = "lang = zh-cn; JSESSIONID = 6AF7DA3F6A5FC3ECA39A7485C3FDBBAE ";

$ Header = array (

"Host: login.biuman.com ",

"Referer: http://www.biuman.com ",

);

// The url of the verification code Image

Function getCookie ($ url ){

Global $ cookieFile;

$ Curl = curl_init (); // Start a CURL session

Curl_setopt ($ curl, CURLOPT_URL, $ url); // address to be accessed

Curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, 0); // Check the certificate source

Curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1); // check whether the SSL encryption algorithm exists from the certificate

Curl_setopt ($ curl, CURLOPT_HTTPGET, 1); // send a regular GET request

Curl_setopt ($ curl, CURLOPT_COOKIEJAR, $ cookieFile); // Save the returned cookie information to the file

$ Res = curl_exec ($ curl );

Curl_close ($ curl );

}

Function login ($ url ){

Global $ cookieFile, $ header, $ agent;

$ Data = array (

'Name' => 'biumany'

);

$ Curl = curl_init (); // Start a CURL session

Curl_setopt ($ curl, CURLOPT_URL, $ url); // address to be accessed

Curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, 0); // Check the certificate source

Curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, 1); // check whether the SSL encryption algorithm exists from the certificate

Curl_setopt ($ curl, CURLOPT_USERAGENT, $ agent); // simulate the browser used by the user

Curl_setopt ($ curl, CURLOPT_HTTPHEADER, $ header); // Set the header information

@ Curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1); // use automatic redirect

// Curl_setopt ($ curl, CURLOPT_HTTPGET, 1); // send a regular GET request

Curl_setopt ($ curl, CURLOPT_POST, 1); // send a regular Post request

Curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data); // data packet submitted by Post

// Curl_setopt ($ curl, CURLOPT_COOKIE, $ cookie); // directly send the cookie content

Curl_setopt ($ curl, CURLOPT_COOKIEFILE, $ cookieFile); // send the Cookie file

Curl_setopt ($ curl, CURLOPT_TIMEOUT, 120); // sets the timeout limit to prevent endless loops.

Curl_setopt ($ curl, CURLOPT_HEADER, 0); // The Returned Header content is not displayed.

Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1); // The obtained information is returned as a file stream.

$ Res = curl_exec ($ curl); // perform the operation

If (curl_errno ($ curl )){

Return 'failed: errno'. curl_error ($ curl );

}

Curl_close ($ curl); // Close the CURL session

Return $ res;

}

// 1.

$ Auth_url = "http://www.biuman.com/auth ";

GetCookie ($ auth_url); // Get the verification code cookie

// 2.

$ Url = 'http: // www.biuman.com/home'; // url submitted after login

$ Res = login ($ url );

Print_r ($ res );

Http://www.bkjia.com/PHPjc/952698.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/952698.htmlTechArticlecurl simulated login implementation tutorial simulated login 126 mailbox, Renren network and so on according to their own modifications, the code is as follows // simulate login verification code of the website, first get the verification code picture c...

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.