Skip to content
Advertisement

Transforming Twilio library request into Guzzle request

unfortunately our project runs on PHP 7.0 and we cannot upgrade it for now. And Twilio’s library uses PHP 7.2+ on the version that contains the trusthub API support.

So I’m trying to do the request “Create EndUser of type: customer_profile_business_information” from this doc page using Guzzle instead of their library, and I’m following instructions from the curl example.

Everything worked well except the Attributes field that looks like it’s being ignored, it’s returning a blank object and of course on their interface it’s also not showing.

So in case the link breaks, the curl code example is the following:

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

And here’s the PHP code that I made:

<?php
// $company is a model
$token = base64_encode(Config::get('twilio.accountSid') . ':' . Config::get('twilio.authToken'));
$client = new GuzzleHttpClient(['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' => '',
        ]
    ]
]);

Is there something I’m missing here that it’s not saving the Attributes data?

PS: the other fields (FriendlyName and Type) are being successfully saved.

Thank you!

Advertisement

Answer

Twilio developer evangelist here.

The Attributes property of Twilio resources tends to be a JSON string, and I think that’s the case for this one too. So, rather than passing an array of attributes, you need to json_encode the array first. This should work for you:

<?php
// $company is a model
$token = base64_encode(Config::get('twilio.accountSid') . ':' . Config::get('twilio.authToken'));
$client = new GuzzleHttpClient(['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' => '',
        ])
    ]
]);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement