美文网首页
php模拟登陆抓取

php模拟登陆抓取

作者: 木头石头锤子 | 来源:发表于2017-03-14 16:01 被阅读0次

    此为在ci框架的抓取程序:

    <?php
    defined( 'BASEPATH' ) OR exit( 'No direct script access allowed' );
    
    class Login extends CI_Controller {
        public function index() {
           $this->checklogin() ;
           var_dump($this->crab());
        }
    
        // 登录
        private function checklogin() {
            $curlPost = 'username=111&password=111';
            $con = $this->curl_post( 'http://127.0.0.1/phc/index/login',$curlPost,1 );
            return $con;
        }
    
        //抓取
        private function crab(){
            $con = $this->curl_post( 'http://127.0.0.1/phc/index/list','',2 );
            return $con;
        }
    
        //通信
        private function curl_post( $url,$curlPost,$method) {
            $cookie_file = dirname(__FILE__).'/cookie.txt';
            $ch = curl_init();//初始化curl
            curl_setopt( $ch, CURLOPT_URL, $url );//抓取指定网页
            curl_setopt( $ch, CURLOPT_HEADER, 0 );//设置header
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );//要求结果为字符串且输出到屏幕上
            curl_setopt( $ch, CURLOPT_POST, 1 ); //post提交方式
            curl_setopt( $ch, CURLOPT_POSTFIELDS, $curlPost );
            if($method == 1)
            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
            else
            curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
            $data = curl_exec( $ch ); //运行curl
            curl_close( $ch );
            if($method != 1) return $data;
        }
    }
    

    此为phalcon登陆验证

    <?php
    namespace home\controllers;
    use Phalcon\Mvc\Controller;
    use common\libs\api;
    
    class indexcontroller extends Controller {
        public function listAction(){
            if($this->session->has('userid')){
                exit("has login");
            }else{
                exit("no login");
            }
        }
        public function loginAction(){
            $username = $this->request->getPost('username');
            $password = $this->request->getPost('password');
            if($username == '111' && $password == '111'){
                $this->session->set('userid',23);
            }
            $this->view->disable();
        }
    }
    

    验证:

    Paste_Image.png Paste_Image.png

    相关文章

      网友评论

          本文标题:php模拟登陆抓取

          本文链接:https://www.haomeiwen.com/subject/mdrhnttx.html