枪口-带有外部API POST请求的Laravel问题

问题描述 投票:0回答:1

我正在尝试连接到以下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": "[email protected]",
  "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"
    }
  ]
}

并且我正在尝试用(与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."
],
}

可能我没有以正确的方式发送template_settings我已经尝试了7个小时,但没有任何幸福结果。

任何人都可以帮忙吗?

谢谢

laravel api guzzle
1个回答
0
投票

AS每个API DOC创建发票期望正文作为json数据。但是您将其作为“ form_params”发送。

您缺少'items'&'withholding_taxes'输入,这些是必填字段。

您可以检查此API DOC参考以创建请求。

将您的代码更改为:

guzzlephp

如果开发中需要任何调试,则可以取消注释行guzzlephp

© www.soinside.com 2019 - 2024. All rights reserved.