PHP:我想通过CURL一起发送json和二进制数据 [英] PHP : I want to send json and binary data together by CURL

查看:44
本文介绍了PHP:我想通过CURL一起发送json和二进制数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用curl发送postdata时,我想一起发送json和二进制数据.

When I send postdata using curl, I want to send json and binary data together.

          $RequestForm = array();
          $RequestForm['snd'] = 'Sender';
          $RequestForm['sndnm'] = 'SenderName';
          $RequestForm['rcvs'] = 'Receivers';

          $binary1 = file_get_contents('./test.pdf');
          $binary2 = file_get_contents('./test.jpg');

          $postbody = array(
            'form' => json_encode($RequestForm),
            'file[0]' => $binary1,
            'file[1]' => $binary2
          );

          curl_setopt($http, CURLOPT_POST, 1);
          curl_setopt($http, CURLOPT_POSTFIELDS, $postbody);

我要输入二进制数据,而不是文件路径.

I want to enter binary data, not the path to the file.

我寻找了各种方法,但是我无法解决它们.

I looked for various ways, but I couldn't solve them.

有解决方案吗?

推荐答案

您不应该阅读文件内容来发送文件...使用@和realpath直接传递文件...像这样

you should not read the file contens to send file... directly pass the file using @ and realpath... like this

$postbody = array(
           'form' => json_encode($RequestForm),
           'file[0]' =>'@'.realpath('test.pdf'),
           'file[1]' => '@'.realpath('test.jpg')
         );

这篇关于PHP:我想通过CURL一起发送json和二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆