将 Twilio 库请求转换为 Guzzle 请求

Transforming Twilio library request into Guzzle request

不幸的是,我们的项目运行在 PHP 7.0 上,我们暂时无法升级它。 Twilio 的库在包含 trusthub API 支持的版本上使用 PHP 7.2+。

所以我正在尝试使用 Guzzle 而不是他们的库从这个 doc page 中执行请求“创建类型的最终用户:customer_profile_business_information”,我正在按照 curl 示例中的说明进行操作.

除了看起来被忽略的 Attributes 字段外,一切都很好,它返回一个空白对象,当然在他们的界面上也没有显示。

所以如果 link 中断,curl 代码示例如下:

ATTRIBUTES=$(cat << EOF
{
    "business_identity": "direct_customer",
    "business_industry": "EDUCATION",
    "business_name": "acme business",
    "business_regions_of_operation": "USA_AND_CANADA",
    "business_registration_identifier": "DUNS",
    "business_registration_number": "123456789",
    "business_type": "Partnership",
    "social_media_profile_urls": "",
    "website_url": "test.com"
}
EOF
)

curl -X POST https://trusthub.twilio.com/v1/EndUsers \
--data-urlencode "Attributes=$ATTRIBUTES" \
--data-urlencode "FriendlyName=friendly name" \
--data-urlencode "Type=customer_profile_business_information" \
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

这是我编写的 PHP 代码:

<?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']]);
$client->post("EndUsers", [
    'form_params' => [
        'FriendlyName' => $company->business_name,
        'Type' => 'customer_profile_business_information',
        'Attributes' => [
            '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' => '',
        ]
    ]
]);

这里有什么我遗漏的,它没有保存 Attributes 数据吗?

PS: 正在成功保存其他字段(FriendlyName 和 Type)。

谢谢!

这里是 Twilio 开发人员布道者。

Twilio 资源的 Attributes 属性 往往是一个 JSON 字符串,我认为这个也是如此。因此,您需要先 json_encode 数组而不是传递属性数组。这应该适合你:

<?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']]);
$client->post("EndUsers", [
    'form_params' => [
        'FriendlyName' => $company->business_name,
        'Type' => 'customer_profile_business_information',
        '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' => '',
        ])
    ]
]);