public function upload(){ //获取上传文件信息,文件名称以自己实际上传文件名称为准 $fileinfo = $_FILE['filename']; //请求参数,依据商户自己的参数为准 $requestParam['version'] = '10'; $requestParam['cmd_id'] = '122'; $requestParam['mer_cust_id'] = '666666'; $requestParam['attach_file'] = new \CURLFile($fileinfo['tmp_name'],$fileinfo['type'],$fileinfo['name']); //发起请求 $param = [ 'requestData' => [ 'cmd_id' => $requestParam['cmd_id'], 'mer_cust_id' => $requestParam['mer_cust_id'], 'version' => $requestParam['version'], 'attach_file' => $requestParam['attach_file'], ], 'headers' => ['Content-type' => 'multipart/form-data;charset=UTF-8'] ]; $is_upload = 1; $requestRes = $this->request($param,$is_upload); return $requestRes ;}
private function request($param, $is_upload=0) { // 请求接口所以参数初始化 $data = [ 'url' => $this->apiUrl, // 接口 url 'requestData' => $is_upload ? $param['requestData'] : http_build_query($param['requestData']), // 请求接口参数 'headers' => $param['headers'] ]; //print_r($data); //debug 打印接口传输数据 $res = $this->httpPostRequest($data['url'], $data['headers'], $data['requestData']); if (empty($res) || $res['info']['http_code'] != '200') { $this->json_ret('20006','http request error',[$res['error']]); } return [ 'status' => $res['info']['http_code'], 'body' => $res['body'] ];}
//curl post 请求 private function httpPostRequest($url = '', $header = array(), $requestData = array()) { $curl = curl_init(); curl_setopt($curl, CURLOPT_HTTPHEADER, $header); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $requestData); $res = curl_exec($curl); $info = curl_getinfo($curl); $error = curl_error($curl); curl_close($curl); return [ 'body' => $res, 'info' => $info, 'error' => $error, ];}