doupa8922 2018-06-18 13:36 采纳率: 100%
浏览 31

为什么我对使用Guzzle的API的帖子请求不起作用?

I want to make a post request to an API to create a new client but its appearing an error:

Client error: `POST https://testname.app.invoicexpress.com/document-type.json?api_key=...` 
resulted in a `404 Not Found` response: <!doctype html> 
 <!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en">
 <![endif]--> <!--[if IE 7]> <html class="n (truncated...)

In the documentation "https://developers.invoicexpress.com/docs/versions/2.0.0/resources/invoices" says that to create a new client along with the invoice the curl command is like:

  curl --request POST \
      --url 'https://account_name.app.invoicexpress.com/:document-type.json?api_key=YOUR%20API%20KEY%20HERE' \
      --header 'accept: application/json' \
      --header 'content-type: application/json' \
      --data '{"invoice":{"date":"03/12/2017","due_date":"03/12/2017","client":{"name":"Client Name","code":"A1"},"items":[{"name":"Item Name","description":"Item Description","unit_price":"100","quantity":"5"}]}}'

But with the code below instead of the curl command, it shows that error.

public function generateInvoice()
{

    $client = new \GuzzleHttp\Client();

    $array = [
        'invoice' => [
            'date' => '03/12/2017',
            'due_date' => '03/12/2017',
            'client' => [
                'name' => 'Client Name',
                'code' => 'A1'
            ],
            'items' => [
                [
                    'name' => 'Item Name',
                    'description' => 'Item Description',
                    'unit_price' => '100',
                    'quantity' => '5'
                ]
            ]
        ]
    ];

    $response = $client->request('POST', 'https://testname.app.invoicexpress.com/invoices.json', [
        'query' => ['api_key' => '...'], 'form_params' => [$array],
    ]);
    dd($response->getStatusCode());

}

Wihout [] and only $array in the 'form_params' shows:

 Client error: 
   `POST https://testname.app.invoicexpress.com/invoices.json?api_key=...` resulted 
   in a `422 Unprocessable Entity` 
   response: {"errors":[{"error":"Items element should be of type    array"}]}
  • 写回答

1条回答 默认 最新

  • dongxing2015 2018-06-18 13:43
    关注

    Try renaming your param 'form_param' with 'json'

    Like : 'form_params' => [$array] to 'json' => [$array]

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!