我使用 Laravel 9 和內置方法Http::withHeaders ($headers)->post($url, $data)。
在 $data 變數中,我將由 http_build_request 產生的字串與“&”分隔符一起傳遞。但是 Laravel 試圖用它創建一個陣列并發送資料。
API 服務回傳一個錯誤。請告訴我如何強制 Laravel 準確傳遞一個字串(!),而不是一個陣列?
我的代碼:
$array = [
'key1' => 'value1',
'key2' => 'value2',
// etc...
];
$headers = [
'Content-Type' => 'application/x-www-form-urlencoded',
'HMAC' => $hmac
];
$data = http_build_query($array, '', '&');
$response = Http::withHeaders($headers)->post($api_url, $data);
return json_decode($response->getBody()));
uj5u.com熱心網友回復:
你試過asForm嗎,
$response = Http::withHeaders($headers)->asForm()->post($api_url, $data);
如果您想使用 application/x-www-form-urlencoded 內容型別發送資料,您應該在發出請求之前呼叫 asForm 方法。
uj5u.com熱心網友回復:
如果您將其作為 x-www-form-urlencoded 發送,我猜您應該能夠在請求正文中傳遞資料。
$response = Http::withHeaders($headers)
->withBody($data)
->asForm()
->post($api_url);
但是,我不確定這是否可行
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/489570.html
