Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Square Connect-当字段作为数组发送时,OrdersApi错误应为\u数组_Php_Square Connect - Fatal编程技术网

Php Square Connect-当字段作为数组发送时,OrdersApi错误应为\u数组

Php Square Connect-当字段作为数组发送时,OrdersApi错误应为\u数组,php,square-connect,Php,Square Connect,我一直在使用square connect api,在创建订单时遇到问题。我需要创建一个订单。下面是有问题的代码 $api = new \SquareConnect\Api\OrdersApi(); $location = $this->get_location(); if (!empty($items)) { if (is_string($items)) { $items = json_decode($items);

我一直在使用square connect api,在创建订单时遇到问题。我需要创建一个订单。下面是有问题的代码

$api = new \SquareConnect\Api\OrdersApi();
    $location = $this->get_location();
    if (!empty($items)) {
        if (is_string($items)) {
            $items = json_decode($items);
        }
        // print_r($items);
        if (!is_array($items)) {
            // echo "Items is not an array -- exiting now";
            return false;
        }
        $order_data = [];
        $order_data['idempotency_key'] = uniqid();
        $order_data['line_items'] = [];
        foreach ($items as $key => $li) {
            $order_data['line_items'][] = [
                'name' => $li->name,
                'base_price_money' => ['amount' => ($li->price * 100)],
                'quantity' => $li->qty
            ];
        }
        $order_data['taxes'] = [
            'type' => 'ADDITIVE',
            'name' => 'State Sales Tax',
            'percentage' => '7'
        ];
        print_r($order_data);
        // exit;
        $apiResponse = $api->createOrder($location, new  \SquareConnect\Model\CreateOrderRequest($order_data));
        $order = $apiResponse->getOrder();
        print_r($apiResponse);
        print_r($order);
    } 
当我提出请求时,我得到以下错误

Message: [HTTP/1.1 400 Bad Request] {"errors":[{"category":"INVALID_REQUEST_ERROR","code":"EXPECTED_ARRAY","detail":"Expected an array.","field":"taxes"}]}
我已经检查了taxes字段是否为数组

["taxes"]=>
  array(3) {
    ["type"]=>
    string(8) "ADDITIVE"
    ["name"]=>
    string(15) "State Sales Tax"
    ["percentage"]=>
    string(1) "7"
  }
如蒙协助,将不胜感激

编辑:打印($items)输出

变量转储(订单数据)


这里的问题是,您需要提供一个tax对象数组,这让人感到困惑,因为您将SDK的隐式对象创建功能用于PHP数组。在JSON中,您的税款如下所示:

"taxes":[
  {
    "type":"additive"
    ...
  }
]
(请注意,这是一个对象数组),您提供:

"taxes":{
  "type":"additive"
  ...
}
因此,如果您只想快速更改代码,您应该执行以下操作:

    $order_data['taxes'] =array(array(
        'type' => 'ADDITIVE',
        'name' => 'State Sales Tax',
        'percentage' => '7'
    ));
如果希望代码更详细,可以执行以下操作:

$order_data = new \SquareConnect\Model\CreateOrderRequest();
$taxes = \SquareConnect\Model\CreateOrderRequestTax();
$taxes->setType('ADDITIVE');
...
$order_data->setTaxes($taxes);

显然,隐式数组方式要简单一些,但在PHP中可能会令人困惑。这有帮助吗?

打印($items)的输出是什么在
json+\u decode($items)
之后,您的代码看起来很正确,但让我们一起再次检查数据。使用print\r($items)编辑OP
    $order_data['taxes'] =array(array(
        'type' => 'ADDITIVE',
        'name' => 'State Sales Tax',
        'percentage' => '7'
    ));
$order_data = new \SquareConnect\Model\CreateOrderRequest();
$taxes = \SquareConnect\Model\CreateOrderRequestTax();
$taxes->setType('ADDITIVE');
...
$order_data->setTaxes($taxes);