Commit 2014758a authored by 蒋林's avatar 蒋林

对外相关接口开发

parent 3a770a54
<?php
/**
* Created by PhpStorm.
* User: LinJiangL
* Date: 16/10/24
* Time: 上午10:54
*/
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Class Suning 苏宁相关接口
*
* @property api_log_model api_log_model
* @property SourceSuning SourceSuning
* @property ReceiveSuning ReceiveSuning
*/
class Suning extends BaseController
{
public function __construct()
{
parent::__construct();
ini_set("max_execution_time", 600);
}
//服务订单作业条件
public function serviceOrderCondition()
{
$param = $this->input->post();
//验证参数
$obj = $this->receiveObj();
$obj->SetValues($param);
$obj->CheckServiceOrderConditionValues();
$values = $obj->GetValues();
$this->saveLog('', 'serviceOrderCondition', $values);
$sourceObj = $this->sourceObj($values['account'], $values['pwd']);
$result = $sourceObj->ServiceOrderCondition($values['type']);
$obj->ReturnMessage(true, '', $result);
}
//服务订单作业查询
public function serviceOrder()
{
$param = $this->input->post();
//验证参数
$obj = $this->receiveObj();
$obj->SetValues($param);
$obj->CheckServiceOrderValues();
$values = $obj->GetValues();
$this->saveLog('', 'serviceOrder', $values);
$sourceObj = $this->sourceObj($values['account'], $values['pwd']);
$result = $sourceObj->ServiceOrder($values['type'], $values);
$obj->ReturnMessage(true, '', $result);
}
//服务订单查询条件
public function selectServiceOrderCondition()
{
$param = $this->input->post();
//验证参数
$obj = $this->receiveObj();
$obj->SetValues($param);
$obj->CheckSelectServiceOrderConditionValues();
$values = $obj->GetValues();
$this->saveLog('', 'SelectServiceOrderCondition', $values);
$sourceObj = $this->sourceObj($values['account'], $values['pwd']);
$result = $sourceObj->SelectServiceOrderCondition();
$obj->ReturnMessage(true, '', $result);
}
//服务订单作业查询
public function selectServiceOrder()
{
$param = $this->input->post();
//验证参数
$obj = $this->receiveObj();
$obj->SetValues($param);
$obj->CheckSelectServiceOrderValues();
$values = $obj->GetValues();
$this->saveLog('', 'selectServiceOrder', $values);
$sourceObj = $this->sourceObj($values['account'], $values['pwd']);
$result = $sourceObj->SelectServiceOrder($values);
$obj->ReturnMessage(true, '', $result);
}
/**
* 保存日志
*
* @param $search
* @param $method
* @param $data
*/
private function saveLog($search, $method, $data)
{
//记录日志
$this->load->model('api_log_model');
$this->api_log_model->add($search, 'suning', $method, $data);
}
private function sourceObj($username, $pwd)
{
$account = [
'username' => $username,
'pwd' => $pwd
];
$this->load->library('Source/SourceSuning', $account, 'SourceSuning');
return $this->SourceSuning;
}
private function receiveObj()
{
$this->load->library('Receive/ReceiveSuning', null, 'ReceiveSuning');
return $this->ReceiveSuning;
}
}
\ No newline at end of file
......@@ -81,7 +81,8 @@ function return_msg($code=true, $msg='', $data=null)
die();
}
function curl_get($url) {
function curl_get($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
......@@ -100,7 +101,8 @@ function curl_get($url) {
}
function curl_post($url, $post) {
function curl_post($url, $post)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
......@@ -108,7 +110,41 @@ function curl_post($url, $post) {
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
$result = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$info = curl_getinfo($curl);
curl_close($curl);
return ['code' => $code, 'data' => $result];
return ['code' => $code, 'data' => $result, 'info' => $info];
}
function phone_code()
{
$mbox = imap_open("{pop.163.com:995/pop3/ssl}INBOX", "wxh__8888@163.com", "WXH888888");
if (!$mbox) {
return_msg(false, '登陆邮箱失败');
}
$num = imap_num_msg($mbox);
$result = [];
if ($num) {
$body = imap_base64(imap_fetchbody($mbox, imap_num_msg($mbox), 1));
$body = iconv('utf8', 'gbk', $body);
preg_match('/\d{6}/', $body, $code);
preg_match('/2\d{3}-[\s\S]{14}/', $body, $date);
if (isset($code[0]) && isset($date[0])) {
$result['code'] = $code[0];
$result['date'] = $date[0];
$result['st'] = time() - strtotime($result['date']);
}
//关闭资源
imap_close($mbox);
}
return $result;
}
<?php
require 'Receive.php';
class ReceiveSuning extends Receive
{
public function CheckServiceOrderConditionValues()
{
$fileds = ['type'];
$this->CheckValues($fileds);
}
public function CheckServiceOrderValues()
{
$fileds = ['type', 'siteBp'];
$this->CheckValues($fileds);
$dfields = [
'siteBp',
'objectIds',
'startTime',
'endTime',
'processTypeName',
'processType',
];
$this->SetDefaultValues($dfields);
if ($this->values['startTime']) {
$this->values['startTime'] = date('Y-m-d', strtotime($this->values['startTime']));
}
if ($this->values['endTime']) {
$this->values['endTime'] = date('Y-m-d', strtotime($this->values['endTime']));
}
}
public function CheckSelectServiceOrderConditionValues()
{
$this->CheckValues();
}
public function CheckSelectServiceOrderValues()
{
$fileds = ['mark', 'siteBp'];
$this->CheckValues($fileds);
$dfields = [
'mark',
'siteBp',
'objectIds',
'phone',
'startTime',
'endTime',
'processTypeName',
'processType',
'orderStatusName',
'orderStatus'
];
$this->SetDefaultValues($dfields);
if ($this->values['startTime']) {
$this->values['startTime'] = date('Y-m-d', strtotime($this->values['startTime']));
}
if ($this->values['endTime']) {
$this->values['endTime'] = date('Y-m-d', strtotime($this->values['endTime']));
}
}
/**
* 检查参数
* @param array $fields
*/
private function CheckValues($fields = [])
{
if (!$this->GetValues()) {
$this->ReturnMessage(false, '参数错误!');
}
if ($this->CheckAppid() == false) {
$this->ReturnMessage(false, 'appid参数错误!');
}
if ($this->CheckSign() == false) {
$this->ReturnMessage(false, '签名错误!');
}
if (!array_key_exists('account', $this->values) || $this->values['account'] == '') {
$this->ReturnMessage(false, "account参数错误!");
} else {
$this->values['account'] = trim(urldecode($this->values['account']));
}
if (!array_key_exists('pwd', $this->values) || $this->values['pwd'] == '') {
$this->ReturnMessage(false, "pwd参数错误!");
} else {
$this->values['pwd'] = trim(urldecode($this->values['pwd']));
}
if ($fields) {
foreach ($fields as $v) {
if (!array_key_exists($v, $this->values) || $this->values[$v] == '') {
$this->ReturnMessage(false, "{$v}参数错误!");
} else {
$this->values[$v] = trim(urldecode($this->values[$v]));
}
}
}
}
/**
* 给指定参数增加默认值
* @param $fields
*/
private function SetDefaultValues($fields)
{
if ($fields) {
foreach ($fields as $v) {
if (!array_key_exists($v, $this->values)) {
$this->values[$v] = '';
} else {
$this->values[$v] = urldecode($this->values[$v]);
}
}
}
}
}
\ No newline at end of file
......@@ -10,6 +10,7 @@ class Source
{
protected static $cookieFilePath = ''; //cookie 文件路径
protected static $captchaFilePath = ''; //验证码 文件路径
protected static $debug = false;
protected function SetUrlCookie($url)
{
......@@ -56,10 +57,22 @@ class Source
return ['code' => $code, 'data' => $result];
}
protected function CurlPostHttps($url, $post, $headers)
protected function CurlPostHttps($url, $post, $headers, $type="")
{
$cacert = APPPATH.'config/cacert.pem';
if ($type == 'json') {
foreach ($headers as $k => $v) {
if (stripos($v, 'Content-Type') !== false) {
$headers[$k] = 'Content-Type: application/json;charset=UTF-8';
}
}
$post = json_encode($post);
} else {
$post = http_build_query($post);
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
......@@ -71,63 +84,86 @@ class Source
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_COOKIEFILE, self::$cookieFilePath);
curl_setopt($curl, CURLOPT_COOKIEJAR, self::$cookieFilePath);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($curl);
$info = curl_getinfo($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($info['http_code'] == 200) {
if ($code == 200) {
$this->ReturnError($result, $url);
}
return ['info' => $info, 'data' => $result];
return ['code' => $code, 'info' => $info, 'data' => $result];
}
protected function GetUrlContents($url, $isCheck = true)
protected function CurlGetHttps($url, $headers)
{
$cacert = APPPATH.'config/cacert.pem';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_CAINFO, $cacert);
curl_setopt($curl, CURLOPT_COOKIEFILE, self::$cookieFilePath);
$rs = curl_exec($curl); //执行cURL抓取页面内容
$result = curl_exec($curl);
$info = curl_getinfo($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($isCheck) {
$this->ReturnError($rs, $url);
if ($code == 200) {
$this->ReturnError($result, $url);
}
return $rs;
return ['code' => $code, 'info' => $info, 'data' => $result];
}
/**
* 获取手机验证码
* @return array
*/
public function GetMailPhoneCode()
protected function HttpsFollowLocation($url, $headers)
{
$mbox = imap_open ("{pop.163.com:995/pop3/ssl}INBOX", "wxh__8888@163.com", "WXH888888");
$cacert = APPPATH.'config/cacert.pem';
$num = imap_num_msg($mbox);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_CAINFO, $cacert);
curl_setopt($curl, CURLOPT_COOKIEFILE, self::$cookieFilePath);
curl_setopt($curl, CURLOPT_COOKIEJAR, self::$cookieFilePath);
$result = curl_exec($curl);
$info = curl_getinfo($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
$result = [];
if ($code == 302) {
return $this->HttpsFollowLocation($info['redirect_url'], $headers);
}
if ($num) {
$body = imap_base64(imap_fetchbody($mbox, imap_num_msg($mbox), 1));
$body = iconv('utf8', 'gbk', $body);
return ['code' => $code, 'info' => $info, 'data' => $result];
}
preg_match('/\d{6}/', $body, $code);
preg_match('/2\d{3}-[\s\S]{14}/', $body, $date);
protected function GetUrlContents($url, $isCheck = true)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_COOKIEFILE, self::$cookieFilePath);
$rs = curl_exec($curl); //执行cURL抓取页面内容
curl_close($curl);
if (isset($code[0]) && isset($date[0])) {
$result['code'] = $code[0];
$result['date'] = $date[0];
$result['st'] = time() - strtotime($result['date']);
}
if ($isCheck) {
$this->ReturnError($rs, $url);
}
return $result;
return $rs;
}
protected function HandelHtmlScript($html)
......@@ -135,6 +171,15 @@ class Source
return str_replace('script', 'scriptt', $html);
}
protected function Debug($function, $data)
{
if (self::$debug) {
echo "{$function}<br/>";
echo "<pre>";
print_r($data);
}
}
protected function ReturnError($html, $url)
{
if (!$html) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment