在php curl多部分/表单数据请求中发送一个文件和json数据
我试图在PHP中以curl请求的形式上传一个文件和json数据。 命令行中的curl可以很好地处理该请求。 这是命令行中的curl请求:在php curl多部分/表单数据请求中发送一个文件和json数据,php,json,curl,mime-types,php-curl,Php,Json,Curl,Mime Types,Php Curl,我试图在PHP中以curl请求的形式上传一个文件和json数据。 命令行中的curl可以很好地处理该请求。 这是命令行中的curl请求: curl -v --basic -u'username' -F file="@documentTest.pdf;type=application/octet-stream" -F data='{"nomDocument":"test.pdf","externalid":"123456"};type=application/json' https://serve
curl -v --basic -u'username' -F file="@documentTest.pdf;type=application/octet-stream" -F data='{"nomDocument":"test.pdf","externalid":"123456"};type=application/json' https://server.com/api/sendDocument
头被设置为multipart/formdata,我想为请求中发送的每个项目设置mime类型:文件的application/octet流和json数据的application/json
这是我的PHP代码
$dataJson = '{"nomDocument":"test.pdf","externalid":"123456"}';
$header = array('Authorization: Basic c2fghEfgfhVDZkOWxfghfghfgUy', 'Content-Type: multipart/form-data');
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt(
$ch, CURLOPT_POSTFIELDS, array(
'file' => new \CurlFile(realpath('documentTest.pdf'), 'application/octet-stream', 'test.pdf'),
'data' => $dataJson
)
);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_URL, 'https://server.com/api/sendDocument');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // false for https
$page_content = curl_exec($ch);
curl_close($ch);
如何为CURLOPT_POSTFIELDS中的数据设置application/json
多谢各位