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

PHP7.0环境调用Twilio TrustHub API时Guzzle请求Attributes字段失效问题

问题原因

你代码中的Attributes字段直接传入了PHP数组,不符合Twilio接口要求:

  • 参考curl示例可以看到,Attributes是先拼接为完整JSON字符串、再做URL编码后提交的
  • Guzzle的form_params配置处理数组类型的值时,会自动将参数名追加[]后缀并展开为多值参数格式,和接口预期的JSON字符串格式不符,因此被Twilio后端忽略

修复方案

将Attributes对应的数组先做JSON序列化,再传入form_params即可,修正后的代码如下:

<?php
// $company is a model
$token = base64_encode(\Config::get('twilio.accountSid') . ':' . \Config::get('twilio.authToken'));
$client = new \GuzzleHttp\Client(['base_uri' => 'https://trusthub.twilio.com/v1/', 'headers' => ['Authorization' => "Basic {$token}", 'Content-Type' => 'application/x-www-form-urlencoded']]);

// 先将Attributes数组转为JSON字符串
$attributes = json_encode([
    'business_name' => $company->business_name,
    'business_identity' => 'direct_customer',
    'business_type' => $company->business_type,
    'business_industry' => $company->industry->twilio_name,
    'business_registration_identifier' => 'EIN',
    'business_registration_number' => $company->tax_id_number,
    'business_regions_of_operation' => $company->region,
    'website_url' => $company->website,
    'social_media_profile_urls' => '',
], JSON_UNESCAPED_UNICODE); // 可选参数,避免中文等特殊字符被转义

$client->post("EndUsers", [
    'form_params' => [
        'FriendlyName' => $company->business_name,
        'Type' => 'customer_profile_business_information',
        'Attributes' => $attributes
    ]
]);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 22:15:01