signname = 'signature'; $api_id = Dever::input('api_id', 1); $channel_api = Dever::db('middleware/channel_api')->find($api_id); if ($channel_api) { $channel = Dever::db('middleware/channel')->find($channel_api['channel_id']); if (!$channel) { Dever::alert('接口错误'); } } else { Dever::alert('接口错误'); } $url = $channel['host'] . $channel_api['uri']; $param = array(); $header = $body = false; $method = 'get'; $json = false; if ($channel_api['method'] == -1) { $channel_api['method'] = $channel['method']; $channel_api['post_method'] = $channel['post_method']; } if ($channel_api['post_method'] == 2) { $method = 'file'; } elseif ($channel_api['method'] == 2) { $method = 'post'; } if ($channel_api['post_method'] == 3) { $json = true; } if ($channel['type']) { $type = explode(',', $channel['type']); if (in_array(1, $type)) { # 请求体 $this->request($body, 'request_body', array('channel_id' => $channel['id']), $channel, $channel_api); } if (in_array(2, $type)) { # 请求头 $this->request($header, 'request_header', array('channel_id' => $channel['id']), $channel, $channel_api); } } if (!$body) { $body = $this->body($channel, $channel_api); } $this->sign($body, $channel, $channel_api); if (strstr($url, '//')) { $url = str_replace('//', '/', $url); } $log['type'] = 'request'; $log['url'] = $url; $log['body'] = $body; $log['method'] = $method; $log['json'] = $json; $log['header'] = $header; Dever::log($log, 'middleware'); $response = Dever::curl($url, $body, $method, $json, $header); $response = $this->response($response, $channel, $channel_api); if ($response['status'] == 1 && $response['data']) { $insert['api_id'] = $channel_api['id']; $insert['data'] = Dever::json_encode($response['data']); $response['id'] = Dever::db('middleware/channel_api_data')->insert($insert); } $response['request'] = $log; return $response; } # 数据响应格式处理 public function response($response, $channel, $channel_api) { $log['type'] = 'response'; $log['data'] = $response; Dever::log($log, 'middleware'); if ($channel['response_type'] == 2) { $response = Dever::json_decode($response); } elseif ($channel['response_type'] == 2) { $response = (array) simplexml_load_string($data); } $msg = ''; $status = 2; if (isset($response[$channel['response_code']])) { $code = Dever::db('middleware/channel_response_code')->find(array('value' => $response[$channel['response_code']])); if ($code && $code['type'] == 1) { $status = 1; } } $msg = isset($response[$channel['response_msg']]) ? $response[$channel['response_msg']] : 'no'; if (strstr($channel['response_data'], '.')) { $temp = explode('.', $channel['response_data']); $data = isset($response[$temp[0]][$temp[1]]) ? $response[$temp[0]][$temp[1]] : (isset($response[$temp[0]]) ? $response[$temp[0]] : false); } else { $data = isset($response[$channel['response_data']]) ? $response[$channel['response_data']] : false; } return array ( 'status' => $status, 'msg' => $msg, 'response' => $response, 'data' => $data, ); } public function sign(&$body, $channel, $channel_api) { $sign = array(); if ($channel['sign_col']) { $col = explode('+', $channel['sign_col']); foreach ($col as $k => $v) { $v = trim($v); if (isset($body[$v]) && $body[$v]) { $sign[$v] = $body[$v]; } elseif (isset($channel[$v]) && $channel[$v]) { $sign[$v] = $channel[$v]; } } } if ($channel_api && $channel_api['sign_col']) { $col = explode('+', $channel_api['sign_col']); foreach ($col as $k => $v) { $v = trim($v); if (isset($body[$v]) && $body[$v]) { $sign[$v] = $body[$v]; } elseif (isset($channel[$v]) && $channel[$v]) { $sign[$v] = $channel[$v]; } } } if ($channel['sign_sort'] == 2) { ksort($sign); } if ($channel['sign_type'] == 1) { $sign = implode('', $sign); } else { foreach ($sign as $k => $v) { if (strstr($v, 'http')) { $v = urlencode($v); } $sign .= $k . '=' . $v . '&'; } $sign = rtrim($sign, '&'); } $test = Dever::input('test'); if ($test == 1) { print_r($sign);die; } # 如果是ssl 这里需要处理一下,后续处理吧 if ($channel['sign_method'] == 2) { $sign = hash("sha256", $sign); } else { $sign = md5($sign); } if ($channel['sign_after'] == 2) { $sign = strtoupper($sign); } elseif ($channel['sign_after'] == 2) { $sign = strtolower($sign); } $body[$this->signname] = $sign; } public function value($data, $channel, $channel_api = array()) { $value = $data['default']; if (strstr($value, 'Dever')) { $value = '$value = '.$value.';'; eval($value); } elseif ($value == '{timestamp}') { $value = Dever::timestamp(); } elseif ($value == '{appkey}') { $value = $channel['appkey']; } elseif ($value == '{appSecret}') { $value = $channel['appsecret']; } elseif ($value == '{signature}') { $this->signname = $data['key']; } elseif ($value == '{api_request}' && $channel_api) { $value = $this->body($channel, $channel_api); } if (!Dever::zero($value) && !$value) { return Dever::input($data['key']); } if ($data['type'] == 1) { $value = (float) $value; } elseif ($data['type'] == 2) { $value = (string) $value; } elseif ($data['type'] == 3 && is_array($value)) { $value = Dever::json_encode($value); } return $value; } public function request(&$data, $table, $where, $channel, $channel_api = array()) { $request_body = Dever::db('middleware/channel_' . $table)->select($where); if ($request_body) { foreach ($request_body as $k => $v) { $value = $this->value($v, $channel, $channel_api); if ($value || Dever::zero($value)) { $data[$v['key']] = $value; } } } } public function body($channel, $channel_api) { $body = array(); $this->request($body, 'api_request', array('api_id' => $channel_api['id']), $channel); if (!$body) { $body = ''; } return $body; } }