When collecting data in PHP, we cannot do it using a simple collection method (such as file_get_contents). However, if we want to simulate a logged-in user and collect and use it, there is no way. We can use the CURL function to simulate login and collect data
I want to say something here. By default, PHP's CURL function is not turned on, so you have to turn it on yourself. You need to remove the " ; " sign in front of ;extension= php_curl.dll in php.ini! ! !
Okay, let me talk about the program last night. Although it was not successful in the end, I still learned something.
The code is as follows | Copy code |
代码如下 | 复制代码 | $login="http://www.phpyu.com/index.php?action=login"; $post_file="user=××&pw=××"; $cookie_file = tempnam('./temp','cookie'); | $login="http://www.phpyu.com/index.php?action=login";$post_file="user=××&pw=× ×"; $cookie_file = tempnam('./temp','cookie'); 代码如下 | 复制代码 | $ch=curl_init($login_url); /////初始化一个CURL对象 curl_setopt($ch,CURLOPT_HEADER,0); |
|
/////Create a unique file name of the temporary file. If successful, the function returns the new temporary file name. On failure, returns false.
The code is as follows | Copy code |
代码如下 | 复制代码 | curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); ///设置不输出在浏览器上 curl_setopt($ch,CURLOPT_POST,1); | $ch=curl_init($login_url); /////Initialize a CURL objectcurl_setopt($ch,CURLOPT_HEADER,0);
| tr>
代码如下 | 复制代码 |
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_file); ////传递一个作为HTTP "POST"操作的所有数据的字符串。 curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); /////把返回来的cookie信息保存在$cookie_jar文件中 curl_exec($ch);///执行 curl_close($ch);////关闭 |
//If you want to include a header in the output, set this option to a non-zero value.
The code is as follows | Copy code |
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); ///Set not to output on the browsercurl_setopt($ch,CURLOPT_POST,1);
|
/////If you want PHP to do a regular HTTP POST, set this option to a non-zero value. This POST is of the ordinary application/x-www-from-urlencoded type, mostly used by HTML forms.
The code is as follows | Copy code |
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_file); ////Pass a string as all the data for the HTTP "POST" operation. curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); /////Save the returned cookie information in the $cookie_jar file 代码如下 | 复制代码 | $url="http://www.phpyu.com/admin/××"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); | curl_exec($ch);///Execute curl_close($ ch);////Close |
The above has completed the simulated login process The next thing to do is to enter the page with permissions. Remember that you are already logged in, and you should save the login credentials cookie.
The code is as follows | Copy code |
$url="http://www.phpyu.com/admin/××";$ch = curl_init($url);curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0); |
////Display this page on the browser, pay special attention here! ! That is, if it is displayed on the browser, the following $contents will become a Boolean type true
The code is as follows | Copy code |
代码如下 | 复制代码 | curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); $contents = curl_exec($ch); | curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); $contents = curl_exec($ch); |
Example 2
The code is as follows | Copy the code |
cookie_path = './'; //Set cookie saving path $vars['username'] = 'Zhang San'; $vars['pwd'] = '123'; //------------- ----------------------- $method_post = true; // URL address submitted by login (absolute address of the action in the form) $url = 'http://****.com/login'; $url = ' ----
$ch = curl_init(); $params[CURLOPT_URL] = $url; //Request url address $params[CURLOPT_HEADER] = true; // Whether to return the response header information $params[CURLOPT_RETURNTRANSFER] = true; // Whether to return the result $params[CURLOPT_FOLLOWLOCATION] = true; // Whether to redirect $params[CURLOPT_USERAGENT] = 'Mozilla/ 5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1'; $postfields = ''; foreach ($vars as $key => $value) { . $ params[CURLOPT_POSTFIELDS] = $postfields; || is_file($_COOKIE['cookie_jar']))) > { $ Cookie_jar = Tempnam ($ Cookie_path, 'Cookie'); // Generate a cookie file $ Params [Curlopt_cookiejar] = $ cookie_jar; // O> SetCookie ('Cookie_jar' , $cookie_jar); //Save cookie path } curl_setopt_array($ch, $params); //Pass in curl parameters $content = curl_exec($ch); //Execute
echo ' '; echo $content; //Output the login result /* //---------Request another address after successful login. If There are multiple loops that can be executed --------- echo ' -------------------------- -------------------------------------------------- --- '; $nexturl = 'http://****.com/test'; $params[CURLOPT_URL] = $nexturl; $params[CURLOPT_POSTFIELDS] = ''; curl_setopt_array($ch, $params); //Pass in curl parameters $content = curl_exec($ch); //Execute echo $content; //Output the request result //---------------- ---------------------------------- */ curl_close($ch); //Close the connection ?>
|
Note: If you are unable to request an https site, it may be because the certificate or domain name cannot be verified. Just add the following two items before curl_setopt_array:
The code is as follows 代码如下 | 复制代码 |
$params[CURLOPT_SSL_VERIFYPEER] = false;
$params[CURLOPT_SSL_VERIFYHOST] = false;
| |
Copy Code |
$params[CURLOPT_SSL_VERIFYPEER] = false; $params[CURLOPT_SSL_VERIFYHOST] = false;
http://www.bkjia.com/PHPjc/444640.htmlwww.bkjia.com trueTechArticleWe use a simple collection method (such as file_get_contents) to collect in php, which is not possible, but if you want to There is no way to simulate logged in users and collect and use it. We can use...
|
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn