Search code examples
phpjsonxero-api

Creating employee in Xero PHP SDK


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 )


Solution

  • 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