向API发布请求以创建新客户端无效

时间:2018-06-17 15:37:27

标签: php laravel api

我想向此API提出发布请求" https://invoicexpress.com/api/invoices/create"创建一个新客户。

在该文档页面中说必填字段为:date,due_date,client和items。

我有以下代码,但我收到错误:

Client error: POST https://nametest.app.invoicexpress.com/invoices.xml?api_key=... 
resulted in a `422 Unprocessable Entity` response: 
<?xml version="1.0" encoding="UTF-8"?> <errors> <error>
Expected &lt;invoice&gt;</error> </errors>

你知道为什么会出现这种错误吗?

代码:

public function createClient(){

        $client = new \GuzzleHttp\Client();
        $response = $client->request('POST', 'https://nametest.app.invoicexpress.com/invoices.xml', [
            'query' => ['api_key' => '...'],
            'date' => date('Y-m-d H:i:s'),
            'due_date' => date('Y-m-d H:i:s'),
            'client'   => 'John',
            'code'  => '',

            'items' => [
                'item' => [
                    'name' => 'item1',
                    'description' => 'item1 desc',
                    'unit_price' => '10',
                    'quantity' => '1'
                ],

                'item' => [
                    'name' => 'item2',
                    'description' => 'item2 des',
                    'unit_price' => '10',
                    'quantity' => '1'
                ]
            ]

        ]);
        dd($response->getStatusCode());

    }

1 个答案:

答案 0 :(得分:0)

文档指出XML中的顶级元素必须为<invoice>,而数据中缺少该元素。它看起来应该更像:

$response = $client->request('POST', 'https://nametest.app.invoicexpress.com/invoices.xml', [
    'invoice' => [
        'query' => ['api_key' => '...'],
        ...
    ]
]);

另请注意,您已获得'client' => 'John',,但文档表明它应该更像'client' => ['name' => 'John'],