在Xero PHP SDK中创建员工

发布于 2025-01-28 21:24:37 字数 1695 浏览 2 评论 0原文

我已经从Xero开发人员网站复制了示例代码,但请继续遇到dateofth的错误 代码

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure OAuth2 access token for authorization: OAuth2
$config = XeroAPI\XeroPHP\Configuration::getDefaultConfiguration()->setAccessToken( 'YOUR_ACCESS_TOKEN' );       

$apiInstance = new XeroAPI\XeroPHP\Api\PayrollAuApi(
    new GuzzleHttp\Client(),
    $config
);
$xeroTenantId = "xeroTenantId_example";
$dateOfBirth = new DateTime('2000-10-28');

$homeAddress = new XeroAPI\XeroPHP\Models\PayrollAu\HomeAddress;
$homeAddress->setAddressLine1('123 Test st');
$homeAddress->setRegion('VIC');
$homeAddress->setPostalCode(3000);
$homeAddress->setCity('Melbourne');

$employee = new XeroAPI\XeroPHP\Models\PayrollAu\Employee;
$employee->setFirstName('Adam');
$employee->setLastName('Adamson');
$employee->setDateOfBirth($dateOfBirth);
$employee->setHomeAddress($homeAddress);

try {
  $result = $apiInstance->createEmployee($xeroTenantId, $employee);
} catch (Exception $e) {
  echo 'Exception when calling PayrollAuApi->createEmployee: ', $e->getMessage(), PHP_EOL;
}
?>

这是我遇到的错误的 json de/serialization期间发生了不良要求。无法对当前的JSON对象(例如{“ name”:“ value”})进行'paycycle.api.dto.au.employee.updateEmployeeReeReequest',因为类型需要JSON数组(例如[1,2,3])正确化。要解决此错误要么将JSON更改为JSON阵列(例如[1,2,3])或更改供应类型,因此它是正常的.NET类型(例如,不是像Integer这样的原始类型,而不是类型的集合类型可以从JSON对象进行值得序列化的数组或列表。 JonobjectAttribute也可以将其添加到类型中,以强迫其从JSON对象进行验证。路径“ dateofbirth',第1行,位置15。

这是从Xeroapi \ Xerophp \ models \ payrollau \雇用对象

[date_of_birth] =&gt; gt; dateTime对象([[date] =&gt; 2000-10-28 00:00:00.000000 [TimeZone_Type] =&gt; 3 [TimeZone] =&GT; Australia/NSW)

I have copied the sample code from the Xero developers site but keep getting an error with the DateOfBirth
here is the code

<?php
require_once(__DIR__ . '/vendor/autoload.php');

// Configure OAuth2 access token for authorization: OAuth2
$config = XeroAPI\XeroPHP\Configuration::getDefaultConfiguration()->setAccessToken( 'YOUR_ACCESS_TOKEN' );       

$apiInstance = new XeroAPI\XeroPHP\Api\PayrollAuApi(
    new GuzzleHttp\Client(),
    $config
);
$xeroTenantId = "xeroTenantId_example";
$dateOfBirth = new DateTime('2000-10-28');

$homeAddress = new XeroAPI\XeroPHP\Models\PayrollAu\HomeAddress;
$homeAddress->setAddressLine1('123 Test st');
$homeAddress->setRegion('VIC');
$homeAddress->setPostalCode(3000);
$homeAddress->setCity('Melbourne');

$employee = new XeroAPI\XeroPHP\Models\PayrollAu\Employee;
$employee->setFirstName('Adam');
$employee->setLastName('Adamson');
$employee->setDateOfBirth($dateOfBirth);
$employee->setHomeAddress($homeAddress);

try {
  $result = $apiInstance->createEmployee($xeroTenantId, $employee);
} catch (Exception $e) {
  echo 'Exception when calling PayrollAuApi->createEmployee: ', $e->getMessage(), PHP_EOL;
}
?>

The error I get is
Bad requestError occurred during JSON de/serialization. Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'Paycycle.API.DTO.AU.Employee.UpdateEmployeeRequest' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'DateOfBirth', line 1, position 15.

This is what is being posted from the XeroAPI\XeroPHP\Models\PayrollAu\Employee Object

[date_of_birth] => DateTime Object ( [date] => 2000-10-28 00:00:00.000000 [timezone_type] => 3 [timezone] => Australia/NSW )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

池木 2025-02-04 21:24:37

像这样的员工通过和一系列员工对我有用。

[{
      "FirstName": "Joe",
      "LastName": "Baba",
      "DateOfBirth": "1998-09-01",
      "HomeAddress": {
        "AddressLine1": "123 Main St",
        "AddressLine2": "St. Kilda",
        "City": "Waiwhetu",
        "Region": "ACT",
        "PostalCode": "3182",
        "Country": "AUSTRALIA"
      }
    }
]

这是成功执行的POST方法的屏幕截图。

“

Passing and array of employees like this worked for me.

[{
      "FirstName": "Joe",
      "LastName": "Baba",
      "DateOfBirth": "1998-09-01",
      "HomeAddress": {
        "AddressLine1": "123 Main St",
        "AddressLine2": "St. Kilda",
        "City": "Waiwhetu",
        "Region": "ACT",
        "PostalCode": "3182",
        "Country": "AUSTRALIA"
      }
    }
]

Here is the screenshot of the post method that executed successfully.

1

晨敛清荷 2025-02-04 21:24:37

也许通过员工的数组

The documentation doesn't look much different.

Maybe pass an array of employees instead?

初见你 2025-02-04 21:24:37

是的,对它进行了排序,我只需要将员工放入数组中

$newEmployees = [];     
array_push($newEmployees, $employee);

$result = $apiInstance->createEmployee($xeroTenantId, $newEmployees);

yep, got it sorted, I just needed to put the employee into an array

$newEmployees = [];     
array_push($newEmployees, $employee);

$result = $apiInstance->createEmployee($xeroTenantId, $newEmployees);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文