You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

xero-php-oauth2搜索功能异常:无法筛选单个Xero联系人

问题原因及修复方案

你的代码核心问题出在搜索参数的传递方式错误:

你把$searchTerm定义成了带参数名的字符串"searchTerm=ben.bowden@24locks.com",但Xero PHP SDK的getContacts方法的第二个参数只需要传入搜索关键词本身,不需要额外添加searchTerm=前缀。另外,若要精确匹配邮箱,使用where参数会比模糊匹配的searchTerm更可靠。

修复后的代码示例

方式1:使用searchTerm模糊匹配

$storage = new StorageClass();
$xeroTenantId = (string)$storage->getSession()->tenant_id;
 
$config = XeroAPI\XeroPHP\Configuration::getDefaultConfiguration()->setAccessToken( (string)$storage->getSession()->token );

$apiInstance = new XeroAPI\XeroPHP\Api\AccountingApi(
    new GuzzleHttp\Client(),
    $config
);
// 仅传入邮箱地址作为搜索关键词,无需添加searchTerm=前缀
$searchTerm = "ben.bowden@24locks.com";

try {
  $result = $apiInstance->getContacts($xeroTenantId, $searchTerm);
  print_r($result);
} catch (Exception $e) {
  echo 'Exception when calling AccountingApi->getContacts: ', $e->getMessage(), PHP_EOL;
}

方式2:使用where参数精确匹配邮箱

如果需要严格匹配邮箱地址,避免模糊匹配带来的无关结果,建议使用where参数:

$storage = new StorageClass();
$xeroTenantId = (string)$storage->getSession()->tenant_id;
 
$config = XeroAPI\XeroPHP\Configuration::getDefaultConfiguration()->setAccessToken( (string)$storage->getSession()->token );

$apiInstance = new XeroAPI\XeroPHP\Api\AccountingApi(
    new GuzzleHttp\Client(),
    $config
);
// 用Xero查询语法精确匹配EmailAddress字段
$whereClause = "EmailAddress == 'ben.bowden@24locks.com'";

try {
  // 第三个参数传入where条件,第二个searchTerm参数留空
  $result = $apiInstance->getContacts($xeroTenantId, null, $whereClause);
  print_r($result);
} catch (Exception $e) {
  echo 'Exception when calling AccountingApi->getContacts: ', $e->getMessage(), PHP_EOL;
}

补充说明

  • searchTerm会在联系人的Name、EmailAddress、ContactNumber等字段中做模糊搜索,可能返回多个匹配结果;
  • where参数支持精准的筛选逻辑,符合Xero API的查询语法,更适合需要精确匹配的场景。

内容的提问来源于stack exchange,提问作者Scott Laurie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 08:03:56