首页 > 解决方案 > PHP CURL Multipart 表单数据使用上传问题

问题描述

我正在尝试将图片上传到 Api 网站,但网站回答没有上传图片。我测试了所有代码,但没有结果。请帮我

标签: phpform-databoundary

解决方案


这是我的脚本代码:

            $BOUNDARY = $this->randomPassword();
            $BODY="";
            $BODY .= '------WebKitFormBoundary'.$BOUNDARY. $eol;
            $BODY .= 'Content-Disposition: form-data; name="flowChunkNumber"' . $eol . $eol;
            $BODY .= "1" . $eol;
            $BODY .= '------WebKitFormBoundary'.$BOUNDARY. $eol;
            $BODY .= 'Content-Disposition: form-data; name="flowChunkSize"' . $eol . $eol;
            $BODY .= "15728640" . $eol;
            $BODY .= '------WebKitFormBoundary'.$BOUNDARY. $eol;
            $BODY .= 'Content-Disposition: form-data; name="flowCurrentChunkSize"' . $eol . $eol;
            $BODY .= "2382085" . $eol;      
            $BODY .= '------WebKitFormBoundary'.$BOUNDARY. $eol;
            $BODY .= 'Content-Disposition: form-data; name="flowTotalSize"' . $eol . $eol;
            $BODY .= "2382085" . $eol;      
            $BODY .= '------WebKitFormBoundary'.$BOUNDARY. $eol;
            $BODY .= 'Content-Disposition: form-data; name="flowIdentifier"' . $eol . $eol;
            $BODY .= "2382085-111jpg" . $eol;       
            $BODY .= '------WebKitFormBoundary'.$BOUNDARY. $eol;
            $BODY .= 'Content-Disposition: form-data; name="flowFilename"' . $eol . $eol;
            $BODY .= "111.jpg" . $eol;          
            $BODY .= '------WebKitFormBoundary'.$BOUNDARY. $eol;
            $BODY .= 'Content-Disposition: form-data; name="flowRelativePath"' . $eol . $eol;
            $BODY .= "111.jpg" . $eol;  
            $BODY .= '------WebKitFormBoundary'.$BOUNDARY. $eol;
            $BODY .= 'Content-Disposition: form-data; name="flowTotalChunks"' . $eol . $eol;
            $BODY .= "1" . $eol;        
            $BODY .= '------WebKitFormBoundary'.$BOUNDARY. $eol;
            $BODY .= 'Content-Disposition: form-data; name="flowTotalChunks"' . $eol . $eol;
            $BODY .= "1" . $eol;    
            $BODY .= '------WebKitFormBoundary'.$BOUNDARY. $eol;
            $BODY .= 'Content-Disposition: form-data; name="file"; filename="111.jpg"'. $eol ;
            $BODY .= 'Content-Type: image/jpeg' . $eol . $eol;
            //$BODY.= 'Content-Transfer-Encoding: base64' . $eol . $eol;
            $BODY .= chunk_split(file_get_contents("111.jpg")) . $eol;
            $BODY .= '------WebKitFormBoundary'.$BOUNDARY. $eol;


        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "multipart/form-data; boundary=----WebKitFormBoundary".$BOUNDARY,
            'X-AUTH-TOKEN: '.$Token.'',
            'X-Requested-With: XMLHttpRequest'           
            ));
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/1.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0');
        curl_setopt($ch, CURLOPT_URL, "https://sitename.php/api/upload.php?/dealer/starterpack/upload/".$TransID."");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_REFERER, "https://sitename.php/starter-starterkit");
        curl_setopt($ch, CURLOPT_POST, true); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, $BODY);
        $response = curl_exec($ch);
        echo $response;


推荐阅读