0

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 \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' => '',
        ]
    ]
]);

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!

Brayan
  • 460
  • 2
  • 5
  • 17

1 Answers1

1

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 \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' => '',
        ])
    ]
]);
philnash
  • 70,667
  • 10
  • 60
  • 88
  • I've tried that before, and got the error 20500 (Twilio Internal Server Error) – Brayan Oct 12 '21 at 04:45
  • Oh, weird. I mean, that's [what the Twilio PHP library does too](https://github.com/twilio/twilio-php/blob/main/src/Twilio/Rest/Trusthub/V1/EndUserList.php#L51) so I'm not sure what else to suggest. Can you try it again with the above code? – philnash Oct 12 '21 at 04:49
  • Yeah, just did it and got same 20500 error :( – Brayan Oct 12 '21 at 05:13
  • Are you able to make the example request using `curl`? With the example data or with your own data? It might be that the best thing to do is [contact Twilio support](https://www.twilio.com/help/contact) as a 20500 error is on the Twilio side of things. – philnash Oct 12 '21 at 05:21
  • Yes using curl on the terminal as per in the docs works fine. And yeah I opened a ticket to their support a while ago, still waiting to hear back from them. I'll dig in a bit more on Twilio library to see if I spot anything that they do different. Thanks for trying to help me tho :) – Brayan Oct 12 '21 at 05:30
  • If you want to email the ticket number to me at philnash at twilio dot com I can try to follow up on it internally. Even if you're not making the request right, you should get a 400 error, not a 500. So I'd like to see this fixed too! – philnash Oct 12 '21 at 05:32
  • I've fixed it, turns out your solution of adding json_encode was correct. My problem was that the field `$company->region` returns an array in case of multiple regions, and the API wasn't throwing the correct error nor giving me a message of unsupported value. So after I changed to this `'business_regions_of_operation' => implode(',', $company->region), ` it fixed the 20500 error :) Thank you! – Brayan Oct 12 '21 at 17:37
  • Glad to hear it's sorted! – philnash Oct 12 '21 at 21:36
  • I'm following up to try to fix the 20500 error now. – philnash Oct 12 '21 at 22:19