Quickbooks PHP JSON数组变量?

问题描述 投票:0回答:1

我试图使用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" => "[email protected]"
  ],
  "BillEmailBcc" => [
        "Address" => "[email protected]"
  ]
]);

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

尝试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" => "[email protected]"
  ],
  "BillEmailBcc" => [
        "Address" => "[email protected]"
  ]
]);

这是尝试#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

这是失败的尝试#2,用json_encode编码的数组。

$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" => "[email protected]"
  ],
  "BillEmailBcc" => [
        "Address" => "[email protected]"      ]

]);

这是第二次尝试失败后的错误信息。

[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 "值的变量,我会非常感激。

php sdk quickbooks-online
1个回答
1
投票

似乎你需要推送 $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" => "[email protected]"
    ],
    "BillEmailBcc" => [
        "Address" => "[email protected]"
    ]
]);
© www.soinside.com 2019 - 2024. All rights reserved.