PHP : I want to send json and binary data together by CURL

梦想与她 提交于 2021-02-17 07:13:51

问题


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.

Is there a solution?


回答1:


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')
         );


来源:https://stackoverflow.com/questions/64038388/php-i-want-to-send-json-and-binary-data-together-by-curl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!