[PHP CURL使用上传问题的多部分表单数据

问题描述 投票:0回答:1

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

php form-data boundary
1个回答
0
投票

这是我的脚本代码:

            $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;

© www.soinside.com 2019 - 2024. All rights reserved.