curl向远程服务器传输file⽂件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']
];
curl命令发送post请求带参数//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,
]
;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论