【问题标题】:How to create array in Guzzle Request?如何在 Guzzle Request 中创建数组?
【发布时间】:2018-01-09 18:48:02
【问题描述】:

我正在尝试向 API 发送帖子,但返回“items should be an array”错误。

  $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 运行时,这段代码就像魅力一样。

有什么建议吗?提前感谢社区!

【问题讨论】:

    标签: php arrays laravel curl guzzle


    【解决方案1】:

    每个项目都应该是一个数组,有自己的描述、数量和 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
            )
    
    )
    

    【讨论】:

    • 我发现了错误!它是“json”而不是“form_params”。
    猜你喜欢
    • 1970-01-01
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    相关资源
    最近更新 更多