[英]Guzzle - Laravel Problem with external API POST request
我試圖連接到以下 api
{
"draft": false,
"documenttype": "4400000040000123",
"series": "4400000040000456",
"number": "1",
"date": "2018-10-25",
"due_date": "2018-11-25",
"client": "3300000040000100",
"client_display_name": "Acme Inc.",
"client_address": "4 Catherine St., Southaven, MS 38671, USA",
"client_shipping_address": "",
"client_profession": "",
"client_vat_number": "",
"client_tax_office": "string",
"client_contact_person": "John Doe",
"client_phone_number": "662-222-2222",
"client_email": "john@doe.com",
"calculator_mode": "initial",
"items": [
{
"product": "9900000040000600",
"title": "Pair of socks",
"description": "Super-cool expensive socks!",
"quantity": "2.00",
"unit_value": "40.00",
"unit_discount": "0.00",
"taxes": [
"6600230050000705"
],
"unit_total": "50.00",
"unit_measure": 14
}
],
"withholding_taxes": [
"6600230050000704"
],
"currency_code": "USD",
"exchange_rate": "1.000000",
"terms": "Terms and conditions here",
"public_notes": "Notes visible on the invoice",
"notes": "Some notes",
"template_settings": {
"discount_appearance": 0,
"hide_client_due": true,
"hide_contact_information": true,
"hide_creator_information": true,
"hide_description": true,
"hide_payments": true,
"hide_product_code": true,
"hide_quantity": true,
"hide_vat": true,
"hide_vat_table": true,
"theme": "1100000022000129",
"unit_measure_appearance": 0
},
"trackingcategories": [
{
"trackingcategory": "7000230020000553",
"option": "My custom tag"
}
]
}
我正在嘗試使用 guzzle(使用 laravel)使用以下代碼進行發布請求:
$headers = ['authorization' => 'Token XXXXXXXXXXXXXXXXXX','x-elorus-organization' => 'XXXXXXXXXXXXXXXXXX','cookie' => 'gwshow=do',];
$client = new \GuzzleHttp\Client();
$url = "https://api.elorus.com/v1.0/invoices/";
$myBody['company'] = "Demo";
$request = $client->post($url, [
'form_params'=>[
'draft'=>true,
'documenttype' => 'XXXXXXXXXXXXXX',
'number' => 0,
'date' => '2020-03-18',
'client' => 'XXXXXXXXXXXX',
'calculator_mode' => 'initial',
'template_settings' => array(
"discount_appearance" => 0,
"hide_client_due" => true,
"hide_contact_information" => true,
"hide_creator_information" => true,
"hide_description" => true,
"hide_payments" => true,
"hide_product_code" => true,
"hide_quantity" => true,
"hide_vat" => true,
"hide_vat_table" => true,
"theme" => "1100000022000129",
"unit_measure_appearance" => 0),
],
'headers' => $headers]);
echo $request->getBody();
所以當我執行代碼時,我收回了以下錯誤
{
"template_settings": [
"This field is required."
],
}
可能我沒有以正確的方式發送我 7 小時前一直在嘗試的模板設置,但沒有任何快樂的結果。
有人可以幫忙嗎?
謝謝
按照API DOC
創建發票,期望正文為 json 數據。 但是您將其作為“form_params”發送。
您缺少 'items' 和 'withholding_taxes' 輸入,這些是必填字段。
您可以查看此guzzlephp
參考以創建請求。
將您的代碼更改為:
$request = $client->POST($url, [
/*'debug' => fopen('php://stderr', 'w'),*/
'json'=>[
'draft'=>true,
'documenttype' => 'XXXXXXX',
'number' => 0,
'date' => '2020-03-18',
'client' => 'XXXXXXX',
'calculator_mode' => 'initial',
'template_settings' => array(
"discount_appearance" => 0,
"hide_client_due" => true,
"hide_contact_information" => true,
"hide_creator_information" => true,
"hide_description" => true,
"hide_payments" => true,
"hide_product_code" => true,
"hide_quantity" => true,
"hide_vat" => true,
"hide_vat_table" => true,
"theme" => "1100000022000129",
"unit_measure_appearance" => 0),
],
'headers' => $headers
]);
如果開發中需要任何調試/*'debug' => fopen('php://stderr', 'w'),*/
您可以取消注釋/*'debug' => fopen('php://stderr', 'w'),*/
。
您可以使用 Postman 檢查 Elorus API,並可以將結果與 Laravel 應用程序匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.