简体   繁体   English

在 Xero 中创建联系人并使用 php api (oauth2) 获取其联系人 ID

[英]Create a contact in Xero and get its contact Id using php api (oauth2)

Just learning Xero API (php) but I'm unsure how to proceed.刚刚学习 Xero API (php) 但我不确定如何继续。 The docs are pretty good in general.总的来说,这些文档非常好。 I've successfully created the oauth2 integration and this connects no problem (even for multiple organisations/tenants), I'm able to get the existing contacts in Xero but I now need to create a new contact (I have the name of this conteact - call her Jane Doe) i then wish to update my database record with this new Contacts contactId.我已经成功创建了 oauth2 集成,并且连接没有问题(即使对于多个组织/租户),我能够在 Xero 中获取现有联系人,但我现在需要创建一个新联系人(我有这个联系人的名字- 叫她 Jane Doe)然后我希望用这个新的 Contacts 联系人 ID 更新我的数据库记录。

So the docs are a bit confusing but looking at the php api i think i can use:所以文档有点混乱,但看看 php api 我想我可以使用:

$response = $accountingApi->setContacts( $xeroTenantId, '{"Name": "Jane Doe"}' );

would this be the right kind of approach (where $accountingApi is defined in a call earlier in the cycle and is connected)?这会是正确的方法吗(其中 $accountingApi 在循环早期的调用中定义并已连接)? does anyone have an example on how to add a new contact to Xero and return this new contacts contactId?有没有人有关于如何向 Xero 添加新联系人并返回这个新联系人contactId 的示例?

The docs don't say what (if any) response is returned after adding a new contact.文档没有说明添加新联系人后返回什么(如果有)响应。

Lastly somewhat related to this, Some of my contacts are in more than one linked organisations, would these have the same clientID or will i need to somehow define one for each connected organisation?最后与此有些相关,我的一些联系人在多个关联组织中,这些联系人是否具有相同的 clientID,或者我需要以某种方式为每个关联组织定义一个?

thanks in advance提前致谢

ADDITONAL附加的

the api docs on github have this snippet: github 上的 api 文档有这个片段:

try {
            $person = new XeroAPI\XeroPHP\Models\Accounting\ContactPerson;
            $person->setFirstName("John")
                ->setLastName("Smith")
                ->setEmailAddress("john.smith@24locks.com")
                ->setIncludeInEmails(true);

            $arr_persons = [];
            array_push($arr_persons, $person);

            $contact = new XeroAPI\XeroPHP\Models\Accounting\Contact;
            $contact->setName('FooBar')
                ->setFirstName("Foo")
                ->setLastName("Bar")
                ->setEmailAddress("ben.bowden@24locks.com")
                ->setContactPersons($arr_persons);

            $arr_contacts = [];
            array_push($arr_contacts, $contact);
            $contacts = new XeroAPI\XeroPHP\Models\Accounting\Contacts;
            $contacts->setContacts($arr_contacts);

            $apiResponse = $accountingApi->createContacts($xeroTenantId,$contacts);
            $message = 'New Contact Name: ' . $apiResponse->getContacts()[0]->getName();
        } catch (\XeroAPI\XeroPHP\ApiException $e) {
            $error = AccountingObjectSerializer::deserialize(
                $e->getResponseBody(),
                '\XeroAPI\XeroPHP\Models\Accounting\Error',
                []
            );
            $message = "ApiException - " . $error->getElements()[0]["validation_errors"][0]["message"];
        }

I require only the name on Xero (all other details are in my linked App) and to obtain the contactId.我只需要 Xero 上的名称(所有其他详细信息都在我的链接应用程序中)并获取 contactId。

Ok so having reviewed the docs I am going with:好的,所以查看了我要使用的文档:

                $contact = new XeroAPI\XeroPHP\Models\Accounting\Contact;
                $contact->setName('Jane Doe');
                $arr_contacts = [];
                array_push($arr_contacts, $contact);
                $contacts = new XeroAPI\XeroPHP\Models\Accounting\Contacts;
                $contacts->setContacts($arr_contacts);

                $apiResponse = $accountingApi->createContacts($xeroTenantId,$contacts);
                //$message = 'New Contact Name: ' . $apiResponse->getContacts()[0]->getName();
                $contactId = $apiResponse->getContacts()[0]->getContactId();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM