php - PHP CURL Multipart 表单数据使用上传问题
问题描述
我正在尝试将图片上传到 Api 网站,但网站回答没有上传图片。我测试了所有代码,但没有结果。请帮我
解决方案
这是我的脚本代码:
$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;
推荐阅读
- php - 用于处理类似“test-12-1”的字符串的正则表达式 (php)
- javascript - 从 webpack 导入后,Vue 应用程序中的 MomentJS 警告
- java - Timer 和 Runnable 有什么区别?
- python - 在 tkinter 条目小部件中输入的数据包含“NoneType”对象并且没有属性“get”
- java - 如何优化初学者 HackerEarth 代码
- azure - 如何在新的 Azure Cloud Power Shell 'Az' 中运行 Azure 的 Get-AzureServiceAntimalwareConfig cmdlet
- ruby - 未定义的方法“gsub”,变量可能存在问题
- android - 类型不匹配。必需:NotificationCompat.Style,找到:Notification.BigPictureStyle
- javascript - 它增加了第二个 } 并且不将 1 添加到级别
- javascript - 以特殊顺序对数组进行排序