Home php教程 PHP源码 php模拟登陆的2种实现方法

php模拟登陆的2种实现方法

Jun 08, 2016 pm 05:21 PM
curl quot snoopy

在php中模拟登陆一般会使用到curl来实现了,这个是php自带的一个函数了,我们可以简单的配置一下就能使用了,下面来看看吧。

<script>ec(2);</script>

php模拟登陆的实现方法,这里分别列举两种方法实现模拟登陆人人网。具体实例代码如下:

1)使用snoopy模拟登陆

 代码如下 复制代码

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

(2)使用curl模拟登陆

 代码如下 复制代码

set_time_limit(0);
$cookie_file=tempnam('./tmp','cookie');//tmp目录需要先建立好
$ch=curl_init();
$login_url='http://www.111cn.net/PLogin.do';
$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.111cn.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);

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to realize the mutual conversion between CURL and python requests in python How to realize the mutual conversion between CURL and python requests in python May 03, 2023 pm 12:49 PM

How to realize the mutual conversion between CURL and python requests in python

Tutorial on updating curl version under Linux! Tutorial on updating curl version under Linux! Mar 07, 2024 am 08:30 AM

Tutorial on updating curl version under Linux!

PHP8.1 released: Introducing curl for concurrent processing of multiple requests PHP8.1 released: Introducing curl for concurrent processing of multiple requests Jul 08, 2023 pm 09:13 PM

PHP8.1 released: Introducing curl for concurrent processing of multiple requests

From start to finish: How to use php extension cURL to make HTTP requests From start to finish: How to use php extension cURL to make HTTP requests Jul 29, 2023 pm 05:07 PM

From start to finish: How to use php extension cURL to make HTTP requests

what is linux curl what is linux curl Apr 20, 2023 pm 05:05 PM

what is linux curl

How to handle 301 redirection of web pages in PHP Curl? How to handle 301 redirection of web pages in PHP Curl? Mar 08, 2024 am 11:36 AM

How to handle 301 redirection of web pages in PHP Curl?

How to set cookies in php curl How to set cookies in php curl Sep 26, 2021 am 09:27 AM

How to set cookies in php curl

Solution to PHP Fatal error: Call to undefined function curl_setopt() Solution to PHP Fatal error: Call to undefined function curl_setopt() Jun 23, 2023 am 08:18 AM

Solution to PHP Fatal error: Call to undefined function curl_setopt()

See all articles