Quickbooks PHP JSON 数组变量?

Quickbooks PHP JSON Array Variable?

我正在尝试使用 Quickbooks PHP SDK 提交 API 调用以创建发票。我可以使用硬编码的 JSON 数组调用工作,但是当我尝试使用变量传递它时,调用失败。

这是有效的版本:

$theResourceObj = Invoice::create([
        "Line" => [
            [
                "Amount" => 100.00,
                "DetailType" => "SalesItemLineDetail",
                "SalesItemLineDetail" => [
                    "ItemRef" => [
                        "value" => 1,
                        "name" => "Hours"
                    ]
                ]
            ],
            [
                "Amount" => 200.00,
                "DetailType" => "SalesItemLineDetail",
                "SalesItemLineDetail" => [
                    "ItemRef" => [
                        "value" => 2,
                        "name" => "Hours"
                    ]
                ]
            ]


        ], 
  "CustomerRef"=> [
    "value"=> 2228
  ],
  "BillEmail" => [
        "Address" => "xxx@gmail.com"
  ],
  "BillEmailBcc" => [
        "Address" => "xxx@healthprodentalstaffing.com"
  ]
]);

当我尝试传递一个变量(动态调用需要传递)时,代码失败。

失败的尝试 1 内爆字符串数组

$lines = array();
$line1 = '["Amount" => 100.00,"DetailType" => "SalesItemLineDetail","SalesItemLineDetail" => ["ItemRef" => ["value" => 1,"name" => "Hours"]]]';
$line2 = '["Amount" => 200.00,"DetailType" => "SalesItemLineDetail","SalesItemLineDetail" => ["ItemRef" => ["value" => 2,"name" => "Hours"]]]';

$theResourceObj = Invoice::create([
        "Line" => "[" . implode("," ,$lines) . "]",          
  "CustomerRef"=> [
    "value"=> 2228
  ],
  "BillEmail" => [
        "Address" => "xxx@gmail.com"
  ],
  "BillEmailBcc" => [
        "Address" => "xxx@healthprodentalstaffing.com"
  ]
]);

这是尝试 #1 的错误:

[11-Jun-2020 15:16:28 UTC] PHP Fatal error:  Uncaught QuickBooksOnline\API\Exception\ServiceException: Http Status Code [400]: Request is not made successful. Response Code:[400] with body: [<?xml version="1.0" encoding="UTF-8" standalone="yes"?><IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2020-06-11T08:16:27.998-07:00"><Fault type="ValidationFault"><Error code="2020" element="Line"><Message>Required param missing, need to supply the required value for the API</Message><Detail>Required parameter Line is missing in the request</Detail></Error></Fault></IntuitResponse>].

 thrown in /home/healt640/vendor/quickbooks/v3-php-sdk/src/Core/HttpClients/SyncRestHandler.php on line 214

这是使用 json_encode

编码的数组的失败尝试 #2
$lines = array();
$lines[] = array("Amount" => 200.00, "DetailType" => "SalesItemLineDetail", "SalesLineItemDetail" => array("ItemRef" => array("value" => 2, "name" => "Hours")));
$lines[] = array("Amount" => 200.00, "DetailType" => "SalesItemLineDetail", "SalesLineItemDetail" => array("ItemRef" => array("value" => 2, "name" => "Hours")));
$json_array = json_encode($lines, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
$theResourceObj = Invoice::create([
        "Line" => "[" . $json_array . "]",           
  "CustomerRef"=> [
    "value"=> 2228
  ],
  "BillEmail" => [
        "Address" => "xxx@gmail.com"
  ],
  "BillEmailBcc" => [
        "Address" => "xxx@healthprodentalstaffing.com"      ]

]);

这是失败尝试 #2 的错误消息:

[11-Jun-2020 15:25:44 UTC] PHP Fatal error:  Uncaught QuickBooksOnline\API\Exception\ServiceException: Http Status Code [400]: Request is not made successful. Response Code:[400] with body: [<?xml version="1.0" encoding="UTF-8" standalone="yes"?><IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2020-06-11T08:25:44.568-07:00"><Fault type="ValidationFault"><Error code="2020" element="Line"><Message>Required param missing, need to supply the required value for the API</Message><Detail>Required parameter Line is missing in the request</Detail></Error></Fault></IntuitResponse>].

thrown in /home/healt640/vendor/quickbooks/v3-php-sdk/src/Core/HttpClients/SyncRestHandler.php on line 214

我确定这是一个简单的修复,例如删除引号或其他内容。

如果有人可以回复一个允许为那些 "Line" 值动态传递变量的工作示例,我将不胜感激!

似乎您需要将 $line1$line1 推送到您的 $lines 数组,然后在您的 $theResourceObj 对象上使用它,如下面的发票 API称呼。

快速修复,

  1. 请记住 $line1$line2 应该是数组,而不是字符串。
  2. 无需编码$lines直接发送即可。

代码:

 $lines = array();
 $line1 = ["Amount" => 100.00,"DetailType" => 
       "SalesItemLineDetail","SalesItemLineDetail" => ["ItemRef" => ["value" => 1,"name" => "Hours"]]];
 $line2 = ["Amount" => 200.00,"DetailType" => 
       "SalesItemLineDetail","SalesItemLineDetail" => ["ItemRef" => ["value" => 2,"name" => "Hours"]]];
 $lines[] = $line1;
 $lines[] = $line2;
 $theResourceObj = Invoice::create([
    "Line" => $lines,          
    "CustomerRef"=> [
        "value"=> 2228
    ],
    "BillEmail" => [
        "Address" => "xxx@gmail.com"
    ],
    "BillEmailBcc" => [
        "Address" => "xxx@healthprodentalstaffing.com"
    ]
]);