Home  >  Article  >  Backend Development  >  Example sharing of php using snoopy and curl to simulate login

Example sharing of php using snoopy and curl to simulate login

*文
*文Original
2018-05-17 15:48:572942browse

Speaking of simulated login, many people may think of CURL. In fact, in addition to CURL, snoopy can also implement simulated login. This article analyzes the two implementation methods of snoopy and curl with examples. I hope it will be helpful to everyone.

The implementation method of php simulated login, the specific example code is as follows:

1) Use snoopy to simulate login:

<?php
set_time_limit(0);
require "Snoopy.class.php";
$snoopy=new Snoopy();
$snoopy->referer=&#39;http://www.***.net/&#39;;
$snoopy->agent="Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0";
$submit_vars[&#39;email&#39;] =&#39;登陆账号&#39;;
$submit_vars[&#39;password&#39;] =&#39;登陆密码&#39;;
$url=&#39;http://www.***.net/test/Login.php&#39;;//登陆数据提交的URL地址
$snoopy->submit($url,$submit_vars);
$snoopy->fetch("http://www.***.net/");//希望获取的页面数据
echo $snoopy->results;//m.***.net

2) Use curl to simulate login:

<?php
set_time_limit(0);
$cookie_file=tempnam(&#39;./tmp&#39;,&#39;cookie&#39;);//tmp目录需要先建立好
$ch=curl_init();
$login_url=&#39;http://www.***.net/PLogin.do&#39;;
$curlPost="email=登陆账号&password=登陆密码";
curl_setopt($ch,CURLOPT_URL,$login_url);
//启用时会将头文件的信息作为数据流输出
curl_setopt($ch,CURLOPT_HEADER,0); //设定是否输出页面内容
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1); //设置请求发送方式,post或get,CURLOPT_POST或CURLOPT_GET
curl_setopt($ch,CURLOPT_POSTFIELDS,$curlPost);
curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file); //保存cookie
curl_exec($ch);
curl_close($ch);
$ch=curl_init();
$login_url2="http://www.***.net/";
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); //读取cookie
curl_exec($ch);
curl_close($ch);

Related recommendations:

Detailed explanation of how to troubleshoot php curl errors

PHP CURL Introduction to cookie delivery methods

php snoopy collection class introduction_PHP tutorial


The above is the detailed content of Example sharing of php using snoopy and curl to simulate login. For more information, please follow other related articles on the PHP Chinese website!

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