curl调取接口上传图片
代码如下:
/*
* 调用接口图片上传
* */
public function upload(){
$files = $_FILES['file'];
$url = '';//接口地址
$avatar = curl_file_create($files['tmp_name'], $files['type'],pathinfo($files['tmp_name'],PATHINFO_BASENAME));//使用函数curl_file_create
$postData['avatar'] = $avatar; //参数
$res = curl_post($url,$postData); //post提交数据
if($res){
$data = json_decode($res,true);
return json(['status'=>2,'message'=>'上传成功']);
}
return json(['status'=>1,'message'=>'上传失败']);
}
如图:
下面方法是post提交代码:
/*
* curl_post
* */
function curl_post($url , $data=array()){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// POST数据
curl_setopt($ch, CURLOPT_POST, 1);
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$curlErrNo = curl_errno($ch);
$curlErr = curl_error($ch);
curl_close($ch);
return $output;
}
如图所示:
如下是接收示例
附上链接一条,更加详细https://www.cnblogs.com/xp796/p/8042040.html