php 模拟登陆

<?php

/**

 * 

 * ***优惠卷验证系统

 * @author Administrator

 *

 */

class CmbchinaController extends Controller {


    const NeptuneCodeUrl = 'http://test.com/checkCode.json';


    const NeptuneLoginUrl = 'http://test.com/login.json';


    public $user_id = '快健康';


    public $password = '123456';


    public function actionIndex() {

        $code_num = isset($_GET['code_num']) ? $_GET['code_num'] : '';

        if($code_num && strlen($code_num) > 5) {

            $cookie_file = Yii::app()->basePath . '/runtime/Cmbchinacookie';

            $time = filectime($cookie_file);

            if($time + 120 < time()) {

                $this->login_post(self::NeptuneLoginUrl, array(

                    'userId' => $this->user_id,

                    'password' => $this->password 

                ), $cookie_file);

            }

            

            $result = $this->authNeptuneCode(self::NeptuneCodeUrl, array(

                'codeNo' => $code_num 

            ), $cookie_file);

            $result = json_decode($result, true);

            Response::respJson($result, $result == null ? false : true);

        } else {

            Response::respJson(array(), false, 'param error!');

        }

    }

    //模拟登录

    function login_post($url, $data, $cookie) {

        $curl = curl_init(); //初始化curl模块

        curl_setopt($curl, CURLOPT_URL, $url); //登录提交的地址

        curl_setopt($curl, CURLOPT_HEADER, 0); //是否显示头信息

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //是否自动显示返回的信息

        curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); //设置Cookie信息保存在指定的文件中

        curl_setopt($curl, CURLOPT_POST, 1); //post方式提交

        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); //要提交的信息

        $result = curl_exec($curl); //执行cURL

        curl_close($curl); //关闭cURL资源,并且释放系统资源

    }

    

    //登录成功后获取数据

    function authNeptuneCode($url, $data, $cookie) {

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_HEADER, 0);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //读取cookie

        curl_setopt($ch, CURLOPT_POST, 1); //post方式提交

        curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //要提交的信息

        $rs = curl_exec($ch); //执行cURL抓取页面内容

        curl_close($ch);

        return $rs;

    }

}

gem source problem http://haoqicat.com/blog/15-ru-he-ti-huan-gem-de-source

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值