php curl form-data,在php curl multipart / form-data请求中发送一个文件和json数据

我正在尝试在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?

谢谢 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值