gpt4 book ai didi

php - 在 Laravel 中,发送一个带有 json 对象数据的 post 请求

转载 作者:行者123 更新时间:2023-12-05 01:06:39 24 4
gpt4 key购买 nike

我想在 Laravel 中使用 Http 外观发送请求,但我无法传递 json 对象数据,它必须只是数组。

Http::withHeaders(['Content-Type' => 'application/json'])->withOptions(['headers' => $coockie])->post($this->policy_url . $address, json_encode($data));

我的数据变量值:

$data = [
"document" => [
"customerId" => 1,
"currencyId" => 1,
"settlementPolicyId" => 8,
"storeId" => 16,
"documentPatternId" => 2,
"items" => [
[
"productId" => 193,
"unitId" => 2,
"quantity" => 1,
"storeId" => 16,
"TrackingFactor1" => "1214",
"PartTrackingFactorRef1" => 1053,
"TrackingFactorHasQuantity1" => true,
"TrackingFactorValue" => "‏test",
"fee" => 0
]
]
],
"payments" => [
[
"key" => "Cash",
"amount" => 445810,
"attr" => []
]
]
];

错误:

TypeError Argument 2 passed to Illuminate\Http\Client\PendingRequest::post() must be of the type array, string given

最佳答案

您可以尝试使用 withBody方法

Http::withBody(json_encode($data), 'application/json')
->withOptions([
'headers' => $coockie
])
->post($this->policy_url . $address);

关于php - 在 Laravel 中,发送一个带有 json 对象数据的 post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68610827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com