<?php
namespace Shell\Controller;
use Think\Controller;
use Shell\Model;
use Think\Crypt\Driver\Des;
use Common\Model\QMC;
use Org\Util\Rbac;
use Org\Util\String;
set_time_limit(0);
ini_set('memory_limit', -1);
/*
* 智运接口访问进程每10分钟启动一次,每秒钟扫描一次消息队列
* /usr/bin/php shell.php shell/ToolApiProcessZhiYun/run/begin_date/20170823/end_date/20171011
*/
class ToolApiProcessZhiYunController extends Controller {
protected $_db;
protected $begin_date;
protected $end_date;
protected $begin_time;
protected $end_time;
protected function _befor_run(){
$this->_db = new \Shell\Model\DbFactoryModel('DB_IDC');
//参数接收
$this->begin_date = $_REQUEST['begin_date'];
$this->end_date = $_REQUEST['end_date'];
//数据运行的日期初始化
if($this->end_date && $this->end_date!="shell.php")
{
$this->begin_time = strtotime($this->begin_date);
$this->end_time = strtotime($this->end_date) + 86399;
}
elseif($this->begin_date && $this->begin_date!="shell.php")
{
$this->begin_time = strtotime($this->begin_date);
$this->end_time = $this->begin_time + 86399;
}
else
{
$this->begin_time = strtotime(date("Y-m-d")) - 86400;
$this->end_time = strtotime(date("Y-m-d")) - 1;
}
}
public function run(){
//初始化参数
$this->_befor_run();
echo "[" . date("Y-m-d H:i:s ") . "] start stats ToolApiProcessZhiYun" . "\n";
for(; $this->begin_time <= $this->end_time; $this->begin_time = strtotime("+1 day", $this->begin_time))
{
$cal_dt = date("Ymd", $this->begin_time);
echo "[" . date("Y-m-d H:i:s ") . "] [ToolApiProcessZhiYun] stats in {$cal_dt}" . "\n";
$this->process($cal_dt);
}
echo "[" . date("Y-m-d H:i:s ") . "] stats ToolApiProcessZhiYun complete" . "\n";
}
/*
* 接口调用进程
*/
protected function process($cal_dt){
// $strSql="SET @x:=null;";
// $this->_db->execute($strSql);
// QMC::input($key, $value );//写入队列
// $list = QMC::output($key);//读取队列
// QMC::input('q1', '111' );//写入队列
// QMC::input('q1', '222' );//写入队列
// QMC::input('q1', array("key1"=>"msg1", "key2"=>"msg2"));//写入队列
// $list = QMC::output('q1', 100);//读取队列
// var_dump($list);
$apiUrl = "https://testopen.95155.com/apis";//联调测试环境接口地址
$apiUser = "2866f962-93f6-4790-8a7e-63ccea78d635";//这里需要替换成:您的API账号
$password = "9cg7PHjRfF3954Xx34zz29q6897D8j";//这里需要替换成:您的API账号密码
$client_id = "9940db0b-6007-4447-9cf6-c7e857d2642c";//这里需要替换成:您的客户端ID
$des_key = "CTFOTRV1";//DES加密解密算法的KEY
$token = "ac14f106-3870-4212-8dd3-0e57c5c1ca93";
//登录参数
// $p = "user=".$apiUser."&pwd=".$password;
// $p = $this->encrypt($p, $des_key, $des_key);
// $content_url = $apiUrl."/login/".$p."?client_id=".$client_id;
//查询参数
$p = "token=".$token."&vclN=陕YH0009&timeNearby=24";
$p = $this->encrypt($p, $des_key, $des_key);
$content_url = $apiUrl."/vLastLocationV3/".$p."?client_id=".$client_id;
$ua = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22';
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$content_url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_REFERER,"http://www.teyuntong.com/");
// curl_setopt($ch,CURLOPT_PROXY,$proxyip);
curl_setopt($ch,CURLOPT_USERAGENT,$ua);
// curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
// curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,true);
curl_setopt($ch,CURLOPT_SSLKEY,dirname(__FILE__)."/../Cafile/4__.95155.com.pem");
// curl_setopt($ch,CURLOPT_SSLCERT,dirname(__FILE__)."/../Cafile/4__.95155.com.pem"); //client.
// curl_setopt($ch, CURLOPT_SSLKEY, dirname(__FILE__).'/card_test/apiclient_key.pem');
$res = curl_exec($ch);
curl_close ($ch);
echo $this->decrypt($res, $des_key, $des_key)."\n";
}
/*
* 在采用DES加密算法,cbc模式,pkcs5Padding字符填充方式下,对明文进行加密函数
*/
private function encrypt($input, $ky, $iv) {
$key = $ky;
$iv = $iv; //$iv为加解密向量
$size = 8; //填充块的大小,单位为bite 初始向量iv的位数要和进行pading的分组块大小相等!!!
$input = $this->pkcs5_pad($input, $size); //对明文进行字符填充
$td = mcrypt_module_open(MCRYPT_DES, '', 'cbc', ''); //MCRYPT_DES代表用DES算法加解密;'cbc'代表使用cbc模式进行加解密.
mcrypt_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $input); //对$input进行加密
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = bin2hex($data); //对加密后的密文进行转16进制
return $data;
}
/*
* 在采用DES加密算法,cbc模式,pkcs5Padding字符填充方式,对密文进行解密函数
*/
private function decrypt($crypt, $ky, $iv) {
$crypt = $this->hex2bin($crypt); //16进制转2进制流
$key = $ky;
$iv = $iv; //$iv为加解密向量
$td = mcrypt_module_open(MCRYPT_DES, '', 'cbc', ''); //MCRYPT_DES代表用DES算法加解密;'cbc'代表使用cbc模式进行加解密.
mcrypt_generic_init($td, $key, $iv);
$decrypted_data = mdecrypt_generic($td, $crypt); //对$input进行解密
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$decrypted_data = $this->pkcs5_unpad($decrypted_data); //对解密后的明文进行去掉字符填充
$decrypted_data = rtrim($decrypted_data); //去空格
return $decrypted_data;
}
/*
* 对明文进行给定块大小的字符填充
*/
private function pkcs5_pad($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
/*
* 对解密后的已字符填充的明文进行去掉填充字符
*/
private function pkcs5_unpad($text) {
$pad = ord($text{strlen($text) - 1});
if ($pad > strlen($text))
return false;
return substr($text, 0, -1 * $pad);
}
/*
* 16进制转二进制流
*/
private function hex2bin($str){
$len = strlen($str)/2;
$re = '';
for($i=0;$i<$len;$i++){
$pos = $i*2;
$re .= chr(hexdec(substr($str,$pos,1))<<4) | chr(hexdec(substr($str,$pos+1,1)));
}
return $re;
}
}
评论0