如何在Guzzle Request中创建数组?

时间:2018-01-09 18:48:03

标签: php arrays laravel curl guzzle

我正在尝试向API发送帖子,但会返回“项应该是数组”错误。

  $response2 = $client2->request('POST', 'https://api.iugu.com/v1/invoices?api_token='.$token, [
      'form_params' => [
          'email' => $email,
          'due_date' => $due_date,
          'items' => ['description' =>
            'Item Um',
            'quantity' => 1,
            'price_cents' => 1000
          ],
          'payer' => [
            'cpf_cnpj' => $cpf_cnpj,
            'name' => $name,
            'phone_prefix' => $phone_prefix,
            'phone' => $phone,
            'email' => $email,
            'address' => [
              'zip_code' => $zip_code,
              'street' => $street,
              'number' => $number,
              'district' => $district,
              'city' => $city,
              'state' => $state,
              'country' => 'Brasil',
              'complement' => $complement
            ]
          ]
      ]
  ]);

我已经尝试过几种方式。

      ['items' => 'description' =>
        'Item Um',
        'quantity' => 1,
        'price_cents' => 1000
      ],

但是没有一种方法可以显示我想要的结果。这很奇怪,因为当我使用PHP和CURL lib运行时,这段代码就像魅力一样。

任何消化?在此先感谢社区!

1 个答案:

答案 0 :(得分:0)

每个项目都应该是一个包含各自描述,数量和price_cents键的数组。将每个数据包裹在另外一个数组中,如下所示:

'items' => [
    [
       'description' => 'Item Um',
       'quantity' => 1,
       'price_cents' => 1000
    ],
]

You'll get an array of items now

Array
(
    [0] => Array
        (
            [description] => Item Um
            [quantity] => 1
            [price_cents] => 1000
        )

)