PHP模拟登陆
2008-07-30 08:41
232 查看
<?php
class curl
{
var $cookiePath = '';
var $loginUrl = '';
var $data = array();//data
var $destUrl = '';//dest url
public $sourceUrlContents = '';
protected $destUrlContents = '';
protected $referer;//source url
protected $postData = '';//post data
protected $domain;//domain
function __construct()
{
$this->cookiePath = tempnam('./tmp','cookie');
}
function joinData()
{
$postData = array();
if(is_array($this->data) || sizeof(get_object_vars($this->data)) > 0)
{
foreach ($this->data as $i => $v)
{
$v = urlencode($v);
$postData []= "{$i}={$v}";
}
}
if(is_array($postData))
{
$this->postData = join('&', $postData);
}
}
//set referer
function setReferer($referer = '')
{
if($referer == '')
{
$this->referer = $this->domain;
}else{
$this->referer = $referer;
}
}
//set dest url
function setDestUrl($url = '')
{
if($url == '')
{
$this->destUrl = $this->domain;
}else{
$this->destUrl = $url;
}
}
//get hidden contents
function getHidden($fileds = 'formhash')
{
$contents = @file_get_contents($this->loginUrl);
if($contents)
{
$exp_match = "/<input type=\"hidden\" name=\"formhash\" value=\"(.*)\" \/>/";
preg_match_all($exp_match, $contents, $match);
return ($match[1][0]);
}else{
return ;
}
}
//post data
function curlPost()
{
$this->domain = $this->getHost($this->loginUrl);
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$this->joinData();
//post data to point url
curl_setopt($curl,CURLOPT_URL, $this->loginUrl);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->postData);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_COOKIEJAR,$this->cookiePath);
curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookiePath);
curl_setopt($curl,CURLOPT_REFERER, $this->referer);
curl_setopt($curl,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, true);//获取header信息
//设定是否输出页面内容
curl_setopt($curl, CURLOPT_NOBODY, false);
$this->sourceUrlContents = curl_exec($curl);
curl_close($curl);
unset($curl);
}
//fetch destiny url
function fetchUrl($destUrl = 'http://hi.baidu.com/')
{
$this->destUrl = $destUrl;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $destUrl);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookiePath);
curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookiePath);
$this->destUrlContents = curl_exec($curl);
curl_close($curl);
unset($curl);
return $this->destUrlContents;
}
//get host by url
function getHost($url)
{
$urlArray = parse_url($url);
return 'http://' . $urlArray['host'];
}
function templates ($title = 'Debug information', $msg)
{
return <<<EOF
<fieldset id="fld_deubg" style="padding:10px;">
<legend>{$title}</legend>
<div id="deubg">{$msg}</div>
</fieldset>
EOF;
}
//debug information
function debug($debugType = 'cookie')
{
switch ($debugType)
{
case 'cookie':
return $this->templates('Cookie information', '<pre>' . file_get_contents($this->cookiePath) . '</pre>');
break;
case 'source':
return $this->templates('Source information', $this->sourceUrlContents);
break;
case 'dest':
return $this->templates('Dest information', $this->destUrlContents);
break;
case 'data':
return $this->templates('Post data', $this->postData);
break;
case 'url':
return $this->templates('<p>Url infomation', 'Login url' . $this->loginUrl . '</p><p>' . $this->destUrl . '</p><p>' . $this->referer . '</p>');
}
}//end function
}
$curl = new curl();
$curl->loginUrl = 'https://reg.163.com/logins.jsp';
$userInfo = new stdClass();
$userInfo->username = '******';
$userInfo->password = '******';
$curl->data = $userInfo;
$curl->curlPost();
echo $curl->fetchUrl('http://blog.163.com/openentry/fromurs/blogmsg.do');
?>
class curl
{
var $cookiePath = '';
var $loginUrl = '';
var $data = array();//data
var $destUrl = '';//dest url
public $sourceUrlContents = '';
protected $destUrlContents = '';
protected $referer;//source url
protected $postData = '';//post data
protected $domain;//domain
function __construct()
{
$this->cookiePath = tempnam('./tmp','cookie');
}
function joinData()
{
$postData = array();
if(is_array($this->data) || sizeof(get_object_vars($this->data)) > 0)
{
foreach ($this->data as $i => $v)
{
$v = urlencode($v);
$postData []= "{$i}={$v}";
}
}
if(is_array($postData))
{
$this->postData = join('&', $postData);
}
}
//set referer
function setReferer($referer = '')
{
if($referer == '')
{
$this->referer = $this->domain;
}else{
$this->referer = $referer;
}
}
//set dest url
function setDestUrl($url = '')
{
if($url == '')
{
$this->destUrl = $this->domain;
}else{
$this->destUrl = $url;
}
}
//get hidden contents
function getHidden($fileds = 'formhash')
{
$contents = @file_get_contents($this->loginUrl);
if($contents)
{
$exp_match = "/<input type=\"hidden\" name=\"formhash\" value=\"(.*)\" \/>/";
preg_match_all($exp_match, $contents, $match);
return ($match[1][0]);
}else{
return ;
}
}
//post data
function curlPost()
{
$this->domain = $this->getHost($this->loginUrl);
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$this->joinData();
//post data to point url
curl_setopt($curl,CURLOPT_URL, $this->loginUrl);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $this->postData);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_COOKIEJAR,$this->cookiePath);
curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookiePath);
curl_setopt($curl,CURLOPT_REFERER, $this->referer);
curl_setopt($curl,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, true);//获取header信息
//设定是否输出页面内容
curl_setopt($curl, CURLOPT_NOBODY, false);
$this->sourceUrlContents = curl_exec($curl);
curl_close($curl);
unset($curl);
}
//fetch destiny url
function fetchUrl($destUrl = 'http://hi.baidu.com/')
{
$this->destUrl = $destUrl;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $destUrl);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookiePath);
curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookiePath);
$this->destUrlContents = curl_exec($curl);
curl_close($curl);
unset($curl);
return $this->destUrlContents;
}
//get host by url
function getHost($url)
{
$urlArray = parse_url($url);
return 'http://' . $urlArray['host'];
}
function templates ($title = 'Debug information', $msg)
{
return <<<EOF
<fieldset id="fld_deubg" style="padding:10px;">
<legend>{$title}</legend>
<div id="deubg">{$msg}</div>
</fieldset>
EOF;
}
//debug information
function debug($debugType = 'cookie')
{
switch ($debugType)
{
case 'cookie':
return $this->templates('Cookie information', '<pre>' . file_get_contents($this->cookiePath) . '</pre>');
break;
case 'source':
return $this->templates('Source information', $this->sourceUrlContents);
break;
case 'dest':
return $this->templates('Dest information', $this->destUrlContents);
break;
case 'data':
return $this->templates('Post data', $this->postData);
break;
case 'url':
return $this->templates('<p>Url infomation', 'Login url' . $this->loginUrl . '</p><p>' . $this->destUrl . '</p><p>' . $this->referer . '</p>');
}
}//end function
}
$curl = new curl();
$curl->loginUrl = 'https://reg.163.com/logins.jsp';
$userInfo = new stdClass();
$userInfo->username = '******';
$userInfo->password = '******';
$curl->data = $userInfo;
$curl->curlPost();
echo $curl->fetchUrl('http://blog.163.com/openentry/fromurs/blogmsg.do');
?>
相关文章推荐
- php实现模拟登陆
- PHP 模拟登陆微信公众平台,一键绑定URL和TOKEN的思路
- php 论坛采集程序 模拟登陆,抓取页面 实现代码
- 跟着百度学PHP[17]-PHP扩展CURL的模拟登陆并获取数据
- php中CURL技术模拟登陆抓取数据实战,抓取某校教务处学生成绩。
- Android PHP Okhttp3模拟登陆和注册
- php 模拟登录淘宝taobao阿里妈妈|模拟登录淘宝联盟|curl模拟登录淘宝|模拟登陆淘宝采集数据
- (PHP)微信公众平台模拟登陆和发…
- PHP模拟登陆获取成绩信息
- php 模拟登陆正方教务系统
- php实现模拟登陆
- 网站模拟登陆备忘(php + python)
- PHP模拟登陆获取163邮箱联系人(经测试)
- 【转】PHP 之 CURL 模拟登陆并获取数据
- 赶集网模拟登陆程序PHP
- php模拟登陆新浪微博
- PHP模拟登陆获取163邮箱联系人
- mark- PHP_CURL1之模拟POST登陆