首页 > 解决方案 > Xero 客户端错误:`POST https://api.xero.com/payroll.xro/1.0/Timesheets` 导致 `400 Bad Request` 响应:0Bad request

问题描述

需要帮助来修复Xero SDK v2发布/创建时间表的问题。Xero PHP API 参考https://xeroapi.github.io/xero-php-oauth2/docs/v2/payroll_au/index.html#api-PayrollAu-createTimesheet

我的代码如下

require __DIR__ . '/xero-auth/vendor/autoload.php';
$config = XeroAPI\XeroPHP\Configuration::getDefaultConfiguration()->setAccessToken(xxxxxxxxx);
$apiInstance = new XeroAPI\XeroPHP\Api\PayrollAuApi(
        new GuzzleHttp\Client(),
        $config
    );
 $xeroTenantId = "xxxxxxxxxxxxxxxxxxx";
 $emp_timeSheet =array();
 $startDate = new DateTime('2021-09-20');
 $endDate = new DateTime('2021-10-03');
 $timesheet = new XeroAPI\XeroPHP\Models\PayrollAu\Timesheet; 
 if($timesheet_id != '') $timesheet->setTimesheetId($timesheet_id);
 $timesheet->setEmployeeId($employee_id);
 $timesheet->setStartDate($Start_Date);                 
 $timesheet->setEndDate($End_Date); 
 $timesheet->setStatus(XeroAPI\XeroPHP\Models\PayrollAu\TimesheetStatus::DRAFT);
 if(count($timelines)>0){                    
                foreach ($timelines as $timeline) {
                    $khours = '';
                    $payItem = $this->xero_model->getPayItem($timeline['EarningsRateKey']); 
                    if(!empty($payItem)) {
                        $timesheetLine = new XeroAPI\XeroPHP\Models\PayrollAu\TimesheetLine;
                        $timesheetLine->setEarningsRateId($payItem['EarningsRateID']);
                        foreach ($timeline['hours'] as $hour) { 
                            //$timesheetLine->setNumberOfUnits($hour);
                            $khours .= number_format($hour, 1).',';                          
                        }
                        $khours = rtrim($khours, ',');
                        $timesheetLine->setNumberOfUnits('['.$khours.']');
                        $timesheet->setTimesheetLines(array('TimesheetLines' => $timesheetLine)); 
                                              
                    }
                }                    
            }  
            array_push($emp_timeSheet, $timesheet);
     try { 
        if($timesheet_id == ''){ //print_r($emp_timeSheet); die;
          $result = $apiInstance->createTimesheet($xeroTenantId, $krr);  
        }else{
          $result = $apiInstance->updateTimesheet($xeroTenantId, $timesheet_id, $emp_timeSheet);
        } 

Xero 响应低于 XeroAPI\XeroPHP\ApiException 对象([responseBody:protected] => 0Bad requestError 在 JSON 反序列化过程中发生。无法将当前 JSON 对象(例如 {"name":"value"})反序列化为“Paycycle”类型。 API.DTO.AU.Timesheet.UpdateTimesheetRequest' 因为该类型需要 JSON 数组(例如 [1,2,3])才能正确反序列化。要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2, 3]) 或更改反序列化类型,使其成为可以从 JSON 对象反序列化的普通 .NET 类型(例如,不是整数之类的原始类型,不是数组或 List<T> 之类的集合类型)。JsonObjectAttribute 可以也被添加到类型以强制它从 JSON 对象反序列化。路径'Timesheets',第 1 行,位置 14。[responseHeaders:protected] =>数组([Content-Type] => 数组([0] => text/html)

        [Content-Length] => Array
            (
                [0] => 814
            )

        [Server] => Array
            (
                [0] => nginx
            )

        [Xero-Correlation-Id] => Array
            (
                [0] => 132113121312321211
            )

        [X-AppMinLimit-Remaining] => Array
            (
                [0] => 9998
            )

        [X-MinLimit-Remaining] => Array
            (
                [0] => 58
            )

        [X-DayLimit-Remaining] => Array
            (
                [0] => 4897
            )

        [Expires] => Array
            (
                [0] => Wed, 22 Sep 2021 10:29:05 GMT
            )

        [Cache-Control] => Array
            (
                [0] => max-age=0, no-cache, no-store
            )

        [Pragma] => Array
            (
                [0] => no-cache
            )

        [Date] => Array
            (
                [0] => Wed, 22 Sep 2021 10:29:05 GMT
            )

        [Connection] => Array
            (
                [0] => close
            )

        [X-Client-TLS-ver] => Array
            (
                [0] => tls1.2
            )

    )

[responseObject:protected] => 
[message:protected] => [400] Client error: `POST https://api.xero.com/payroll.xro/1.0/Timesheets` resulted in a `400 Bad Request` response:

0Bad request<Messa(截断...)

[状态] => 错误 [消息] => [400] 客户端错误:POST https://api.xero.com/payroll.xro/1.0/Timesheets导致400 Bad Request响应:0Bad request<Messa (truncated...)

标签: phpjsonxero-apixero

解决方案


推荐阅读