<?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