简体   繁体   English

使用 PHP 中的 curl 发送复杂数据

[英]Send a complex data using curl in PHP

I have searched a lot, and found many related questions and forums related to this, but this one is a challenging one.我搜索了很多,找到了很多与此相关的问题和论坛,但是这个是一个具有挑战性的。

I'm trying to POST a complex array via curl .我正在尝试通过curl一个复杂的数组。 It has to be form-data while the first value in the array is of type JSON .它必须是form-data ,而数组中的第一个值是JSON类型。 The two other values of array are two images which are uploaded and ready to send.数组的另外两个值是已上传并准备发送的两个图像。

I tried to run it in Postman , and works perfectly fine.我尝试在Postman中运行它,并且工作得很好。 I used the generated PHP code from Postman , but it is not working.我使用了从Postman生成的PHP代码,但它不起作用。 Seems like postman is handling some of its tricks without revealing them to us.似乎 postman 正在处理它的一些技巧而不向我们透露它们。

Any way, I'm posting a Postman image to illustrate what I mean:无论如何,我发布了一张 Postman 图像来说明我的意思: 在此处输入图像描述

As you can see, I'm sending the data in form-data tab, my first value ( param1 ) is a JSON with content-type application/json , the second and third values are images uploaded in Postman .如您所见,我在form-data选项卡中发送数据,我的第一个值( param1 )是一个JSONcontent-type application/json ,第二个和第三个值是上传到Postman的图像。

This works just fine in Postman .这在Postman中工作得很好。

The problem is, if I set Content-Type:multipart/form-data in header, the destination server throws an error saying the content-type must be JSON .问题是,如果我在 header 中设置Content-Type:multipart/form-data ,目标服务器会抛出一个错误,指出content-type必须是JSON

If I set the Content-Type:application/json in header, the destination server says content must be of type Multipart.如果我在 header 中设置Content-Type:application/json ,则目标服务器说内容必须是 Multipart 类型。

Somehow, I need to set both content-types.不知何故,我需要设置两种内容类型。 The main one as form-data and the one for param1 as JSON .主要的form-data和 param1 的一个作为JSON

I paste the Postman code as well, may that be a good start for you fellas to help out with the code.我也粘贴了 Postman 代码,这可能是你们帮助代码的一个好的开始。

Postman Code: Postman 代码:


$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'http://xxxxx.com/xxxx/xxx/xxxx',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => array('param1' => '{
   "AgentId":"1414",
   "ContractId":36529,
   "Files":[
      {
         "FileName":"car_card_front_image.png",
         "FileTypeId":2
      },
      {
         "FileName":"car_card_back_image.png",
         "FileTypeId":2
      }
   ]
}','param2'=> new CURLFILE('/C:/images/icons/car_card_back_image.png'),'param3'=> new CURLFILE('/C:/images/icons/car_card_front_image.png')),
  CURLOPT_HTTPHEADER => array(
    'authenticationToken: xxxx-xxx-xx-xxxxxxxx'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

The PHP generated code by postman, is not working . PHP 由 postman 生成的代码不起作用 One of the reasons can be that there's no content-type mentioned in it.原因之一可能是其中没有提到内容类型。

I tried modifying the code, adding content-types in header and in parameter, nothing seems to work.我尝试修改代码,在 header 和参数中添加内容类型,似乎没有任何效果。

If Postman can do it, we should be able it too, right?如果Postman能做到,我们应该也能做到吧?

Go ahead, make as much changes as you would or suggest anything that comes to your mind, I will test them all. Go 前进,尽可能多地进行更改或提出任何您想到的建议,我将对其进行测试。

Cheeeeers...嘿嘿...

May i suggest the ixudrra/curl library?我可以推荐ixudrra/curl库吗?

It would make your life easier....它会让你的生活更轻松......

$response = Curl::to('http://example.org')
        ->withData( array( 'Foo' => 'Bar' ) )
        ->withFile( 'image_1', '/path/to/dir/image1.png', 'image/png', 'imageName1.png' )
        ->withFile( 'image_2', '/path/to/dir/image2.png', 'image/png', 'imageName2.png' )
        ->post();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM