• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

PHP模拟登陆

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

<?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 >
   <legend>{$title}</legend>
   <div >{$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');
?>


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PHP-目录、文件操作发布时间:2022-07-10
下一篇:
PHP 文件夹操作「复制、删除、查看大小、重命名」递归实现发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap