接口上传图片到服务器

    private function FieldsPost($url, $postData,$auth='')
    {
        $fileName=basename($postData['mf']);#本地图片路径
        $ext = strtolower(pathinfo($postData['mf'], PATHINFO_EXTENSION));
        $this->logs('请求地址:' . $url, 'iworkInterface');
        $this->logs('入参数据:' . json_encode($postData), 'iworkInterface');
        $postData['mf']=new \CURLFile($postData['mf'],$ext,$fileName);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($curl, CURLOPT_TIMEOUT, 60);
        $boundary=substr(md5(time()),8,16);
        $headers = array ();
        $headers[] = 'Accept:*/*';
        $headers[] = "Content-Type:multipart/form-data;boundary=" . $boundary;
        $headers[] = 'X-Access-Token:Bearer ' . $auth;
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        $res = curl_exec($curl);
        $this->logs('出参:' . $res, 'iworkInterface');
        $errorno = curl_errno($curl);
        if ($errorno) {
            $this->logs('异常:' . $errorno, 'iworkInterface');
            return $errorno;
        }
        curl_close($curl);
        return json_decode($res, true);
    }

 

posted on 2023-08-02 17:42  长不大的菜鸟  阅读(17)  评论(0编辑  收藏  举报

导航