我正在尝试在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://server.com/api/sendDocument
标头设置为multipart / form-data,我想为请求中发送的每个项设置mime类型:application / octet-stream表示文件AND application / json表示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?
谢谢 .