PHP POST Curl 标头,如何将数组放入数组中? [复制]

Posted

技术标签:

【中文标题】PHP POST Curl 标头,如何将数组放入数组中? [复制]【英文标题】:PHP POST Curl headers, how to put array within array? [duplicate] 【发布时间】:2021-09-02 00:07:35 【问题描述】:

我正在通过 API 向另一个服务发送 HTTP Post 请求以创建订单。 问题是,它需要一些 HTTP 参数,这些参数是数组中的数组。

这些是我现在使用的参数:

"Content-Type: application/json",
"Accept: application/json",
"Stream-Nonce:".$randomstring2,
"Stream-Party:***************",
"Authorization: bearer ".$result_array["access_token"],

我需要添加这个:

"header": 
"orderNo": "ABCD123456",
"orderDate": "2018-07-23",
"requiredDate": "2018-07-31",
"partner": ,
"orderType": "DELIVERY",
"serviceLevel": "STANDARD",
"services": [],
"customer": ,
"customerOrderNo": "ABC1234567890",
"orderNotes": "These notes relate to the order",
"driverNotes": "These notes are for the driver",
"routeInfo": "CC05"
,

这直接来自我正在使用的服务文档。问题在于“标题”:...。

我尝试使用不同的数组格式,如下所示:

'Content-Type' => ' application/json',
'Accept' => ' application/json',
'Stream-Nonce' => ''.$randomstring2,
'Stream-Party' => '***************',
'Authorization' => ' bearer '.$result_array['access_token'],

在这种情况下,我相信只需输入 ... => array('yada yada' => 'yada');但是这样做之后,标头根本不起作用,发送请求时收到禁止消息。

我可以在这里做什么?谢谢。

【问题讨论】:

可能第二组是body发送的,不是http header。建议你用guzzlehttp代替curl 这里的文档:go2stream.net/API/redoc/index.html#tag/Orders/paths/…,它说它是“请求样本”和“有效负载”,不管它是什么,响应表在它下面 正如@nay 所说,看起来这些实际上并不是 HTTP 标头。您链接到那里的文档在REQUEST BODY SCHEMA 下方提到了headercollection。您只需发送一个 JSON 结构,如示例中所示,作为请求 body 内容。 (Stream-NonceStream-Party 明确列在 HEADER PARAMETERS 下面 - 那些 应该是 HTTP 标头。) 【参考方案1】:

我阅读了 go2stream 文档。这在使用 guzzle 时很容易。下面是示例。 如果您遇到一些问题,很可能是错误的发布数据。 如果您还有任何问题,请给我评论。

// composer require guzzlehttp/guzzle
$client = new \GuzzleHttp\Client();
$headers = [
    // "Content-Type" => "application/json",
    "Stream-Nonce" => $randomstring2,
    "Stream-Party" => $streamParty,
    "Authorization" => "bearer ".$result_array["access_token"],
];
$body = [
    "header" =>  [
        "orderNo" =>  "ABCD123456",
        "orderDate" =>  "2018-07-23",
        "requiredDate" =>  "2018-07-31",
        "partner" =>  [],
        "orderType" =>  "DELIVERY",
        "serviceLevel" =>  "STANDARD",
        "services" =>  [],
        "customer" =>  [],
        "customerOrderNo" =>  "ABC1234567890",
        "orderNotes" =>  "These notes relate to the order",
        "driverNotes" =>  "These notes are for the driver",
        "routeInfo" =>  "CC05"
    ],
    "collection" =>  [
        "address" =>  [
          "name" =>  "Proximity Resourcing Ltd",
          "address1" =>  "6 Kerry Hill",
          "address2" =>  "Off Town Street",
          "address3" =>  "Horsforth",
          "address4" =>  "Leeds",
          "address5" =>  "West Yorkshire",
          "country" =>  "GB",
          "postcode" =>  "LS18 4AY",
          "lat" =>  53.837496,
          "long" =>  -1.640644,
          "nuts" =>  "UKE42",
          "locationNotes" =>  "These notes relate to the location"
        ],
        "contact" =>  [
          "name" =>  "Jane Contact",
          "tel1" =>  "0113 000 0000",
          "tel2" =>  "0113 000 0000",
          "mobile" =>  "07801 000000",
          "fax" =>  "0113 000 0000",
          "email" =>  "sales@go2stream.com"
        ],
        "required" =>  [
          "fromDateTime" =>  "2018-07-31T12 => 00 => 00Z",
          "toDateTime" =>  "2018-08-01T17 => 00 => 00Z"
        ],
        "collectionMethod" =>  "NORTH",
        "timeOnSite" =>  [
          "unload" =>  60,
          "assembly" =>  30
        ],
        "bookingRequired" =>  true,
        "items" =>  [
            [
                "sequence" =>  1,
                "parentSequence" =>  2,
                "code" =>  "ABC123",
                "description" =>  "Widget",
                "quantity" =>  10,
                "weight" =>  150,
                "cube" =>  3.5,
                "assemblyTime" =>  30,
                "stockLocation" =>  "LEEDS",
                "onHandDate" =>  "2017-07-30",
                "packageId" =>  "ABCD1234567890",
                "notes" =>  "Include product manual within packaging.",
                "packageType" =>  "PALLET"
            ]
        ]
    ]
];

$response = $client->request('POST',"https => //www.demo.go2stream.net/api/orders/orders",[
    'headers' => $headers,
    'json' => $body,
]);

【讨论】:

以上是关于PHP POST Curl 标头,如何将数组放入数组中? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

查看包含 POST 数据的 cURL 请求标头

PHP cURL 自定义标头

使用 cURL 仅从 HTTP POST 获取响应标头

如何使用 PHP 序列化数组(数组到查询字符串)

如何在 cURL POST 请求中使用数组

如何通过 PHP 搜索 Linkedin?