php模拟表单,上传文件,解决php对接接口上传图片问题。

2 篇文章 0 订阅

先说几个异常: 

Required request part 'file' is not present

Required request part param is not present

Spring File Upload - 'Required request part is not present'

如果你使用php,对接api端的上传文件接口,包括java接口等,出现以上问题,只需要处理相应的参数即可。

header头,可以不写Content-Type:multiple/form-data

上传的文件地址,请务必要进行CURLFile处理。

代码贴下做参考:

$resource = $_SERVER['DOCUMENT_ROOT'].'/files/aaa.jpg';
$headers = ["header"=>array('Authorization:'.$ysyToken)];

$ysyData['file'] = new \CURLFile($resource);
$result = CurlToolkit::requestYsyApi('POST','www.baidu.com',$ysyData,$headers);

curl函数也贴出来做参考

public static function requestYsyApi($method, $url, $params = array(), $conditions = array())
    {
        $conditions['userAgent'] = isset($conditions['userAgent']) ? $conditions['userAgent'] : '';
        $conditions['connectTimeout'] = isset($conditions['connectTimeout']) ? $conditions['connectTimeout'] : 10;
        $conditions['timeout'] = isset($conditions['timeout']) ? $conditions['timeout'] : 10;

        $curl = curl_init();

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_USERAGENT, $conditions['userAgent']);
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $conditions['connectTimeout']);
        curl_setopt($curl, CURLOPT_TIMEOUT, $conditions['timeout']);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        if(isset($conditions['header']) && $conditions['header']){
            curl_setopt($curl, CURLOPT_HEADER, 0);
        }else{
            curl_setopt($curl, CURLOPT_HEADER, 1);
        }

        if ($method == 'POST') {
            curl_setopt($curl, CURLOPT_POST, 1);
            //TODO
            curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
        } elseif ($method == 'PUT') {
            curl_setopt($curl,CURLOPT_HTTPHEADER,array("X-HTTP-Method-Override: $method"));//设置HTTP头信息
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
        } elseif ($method == 'DELETE') {
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
        } elseif ($method == 'PATCH') {
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PATCH');
            curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
        } else {
            if (!empty($params)) {
                $url = $url.(strpos($url, '?') ? '&' : '?').http_build_query($params);
            }
        }
        if(isset($conditions['header']) && $conditions['header']){
            curl_setopt($curl, CURLOPT_HTTPHEADER, $conditions['header']);
        }

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLINFO_HEADER_OUT, true);

        $response = curl_exec($curl);
        $curlinfo = curl_getinfo($curl);


        curl_close($curl);

        if (empty($curlinfo['namelookup_time'])) {
            return array();
        }

        if (isset($conditions['contentType']) && $conditions['contentType'] == 'plain') {
            return $response;
        }

        $body = json_decode($response, true);

        return $body;
    }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值