php模拟登陆抓取

此为在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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,971评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,833评论 19 139
  • 秋天来啦,果子成熟了,兔子去森林里采果子。 小兔子蹦蹦跳跳地来到森林里,森林里的东西真多呀,有蘑菇,有李子,还...
    赖晨煊Ariel阅读 1,652评论 2 5
  • 认识Markdown 在此之前,需要了解的是。Markdown不支持任何格式,所谓的加粗和斜体,只是两种强调语法,...
    风澈vio阅读 4,959评论 8 83
  • 现在很多应用都有下拉刷新和上拉加载更多的功能,经过两个项目的反复摸索,现在算是找到一套比较简单方便的方案。下拉刷新...
    CoffeeKid阅读 3,816评论 0 1

友情链接更多精彩内容