This article mainly introduces the implementation of php simulated login. The example analyzes the implementation methods of snoopy and curl, which has some reference value, if you need it, you can refer to the example in this article to analyze the implementation of php simulated login. Share it with you for your reference. The specific analysis is as follows:
Php simulated login implementation method. here are two methods to simulate login to Renren. The specific instance code is as follows:
1) use snoopy to simulate logon:
The code is as follows:
<? Php
Set_time_limit (0 );
Require "Snoopy. class. php ";
$ Snoopy = new Snoopy ();
$ Snoopy-> referer = 'http: // www.bitsCN.com /';
$ Snoopy-> agent = "Mozilla/5.0 (Windows NT 6.1; rv: 22.0) Gecko/20100101 Firefox/22.0 ";
$ Submit_vars ['email '] = 'logon account ';
$ Submit_vars ['password'] = 'logon password ';
$ Url = 'http: // www.bitscn.com/test/login.php'#//urladdress provided by the login data
$ Snoopy-> submit ($ url, $ submit_vars );
$ Snoopy-> fetch ("http://www.bitsCN.com/"); // page data to be retrieved
Echo $ snoopy-> results; // m.bitsCN.com
2) use curl to simulate login:
The code is as follows:
<? Php
Set_time_limit (0 );
$ Cookie_file = tempnam ('./tmp', 'cooke'); // The tmp directory must be created first.
$ Ch = curl_init ();
$ Login_url = 'http: // www.bitscn.com/plogin.do ';
$ CurlPost = "email = logon account & password = logon password ";
Curl_setopt ($ ch, CURLOPT_URL, $ login_url );
// When enabled, the header file information is output as a data stream.
Curl_setopt ($ ch, CURLOPT_HEADER, 0); // you can specify whether to output the page content.
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_POST, 1); // you can specify the request sending method, such as post or get, CURLOPT_POST, or CURLOPT_GET.
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ curlPost );
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookie_file); // Save the cookie
Curl_exec ($ ch );
Curl_close ($ ch );
$ Ch = curl_init ();
$ Login_url2 = "http://www.bitsCN.com /";
Curl_setopt ($ ch, CURLOPT_URL, $ login_url2 );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 0 );
Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookie_file); // read cookie
Curl_exec ($ ch );
Curl_close ($ ch );
I hope this article will help you with php programming.