zoukankan      html  css  js  c++  java
  • PHP模拟登陆

    <?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');
    ?>

  • 相关阅读:
    第十二章学习笔记
    UVa OJ 107 The Cat in the Hat (戴帽子的猫)
    UVa OJ 123 Searching Quickly (快速查找)
    UVa OJ 119 Greedy Gift Givers (贪婪的送礼者)
    UVa OJ 113 Power of Cryptography (密文的乘方)
    UVa OJ 112 Tree Summing (树的求和)
    UVa OJ 641 Do the Untwist (解密工作)
    UVa OJ 105 The Skyline Problem (地平线问题)
    UVa OJ 100 The 3n + 1 problem (3n + 1问题)
    UVa OJ 121 Pipe Fitters (装管子)
  • 原文地址:https://www.cnblogs.com/dj1232090/p/1256064.html
Copyright © 2011-2022 走看看