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

PHP CURL PATCH请求UCRM API时新增联系人而非更新问题

Fixing UCRM PATCH API: Updating Existing Contacts Instead of Creating New Entries

Hey there, let’s work through this UCRM API issue together—totally get how frustrating it is when you’re trying to update a phone number and end up with duplicate entries instead. Here’s a step-by-step solution to fix this:

1. First, Understand UCRM’s PATCH Logic for Arrays

UCRM’s PATCH API doesn’t automatically match existing contacts entries unless you explicitly reference their unique identifier (usually an id field on each contact object). If you just send a new contact object without this id, the API treats it as a fresh entry to append—hence your duplicate problem.

2. Fetch the Current Contacts Data First

Before making any updates, you need to pull the existing contacts array for the customer/entity you’re modifying. This lets you locate the exact phone number entry you want to update.

Example with JavaScript (adjust for your language of choice):

// Fetch existing customer data to get current contacts
const getExistingCustomer = async (customerId) => {
  const response = await fetch(`/api/v2/customers/${customerId}`, {
    method: 'GET',
    headers: { 'X-Auth-App-Key': 'YOUR_UCRM_API_KEY' }
  });
  return await response.json();
};

3. Modify the Target Contact Entry

Once you have the existing data, find the specific phone contact (filter by type like "phone" or "mobile", or use the entry’s id if you know it) and update its value field with the new number.

const updatePhoneNumber = (existingData, newPhone) => {
  // Locate the existing phone contact entry
  const phoneEntry = existingData.contacts.find(contact => contact.type === 'phone');
  
  if (phoneEntry) {
    // Update the number while preserving the entry's id (critical for UCRM to recognize it)
    phoneEntry.value = newPhone;
  } else {
    // Optional: If no phone entry exists, create one (skip if you don't want new entries)
    existingData.contacts.push({
      type: 'phone',
      value: newPhone,
      isPrimary: true
    });
  }
  
  return existingData;
};

4. Send the PATCH Request with the Modified Array

Now send the PATCH request, including only the updated contacts array. UCRM will use the id field on each entry to match and update existing records instead of creating new ones.

const sendPatchUpdate = async (customerId, updatedData) => {
  const response = await fetch(`/api/v2/customers/${customerId}`, {
    method: 'PATCH',
    headers: {
      'X-Auth-App-Key': 'YOUR_UCRM_API_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ contacts: updatedData.contacts })
  });
  
  return await response.json();
};

5. Critical Pitfalls to Avoid

  • Never omit the id field: Every existing contact entry in UCRM has a unique id. Keep this field intact when modifying the entry—this is how UCRM knows to update instead of create.
  • Double-check contact types: UCRM uses specific type values (like "phone", "mobile", "email")—make sure you’re targeting the correct one for your phone number.
  • Test with a single entry first: Before rolling out to multiple customers, test with one record to confirm the update works without duplicates.

Putting it all together, your workflow would be:

  1. Fetch the existing customer data
  2. Modify the target phone contact entry
  3. Send the PATCH request with the updated contacts array

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 09:08:50