Post credentials using HttpTransport

Using Joomla may involve retrieving data from an external source. This can be achieved in Joomla by using the HttpTransport class as explained in the Making external calls using HttpFactory article. It may be needed to provide some sort of authorization on the external call, this explains how to post credentials using HttpTransport.

Setting up the Authorization header

To post credentials using HttpTransport it is required to use the Authorization header and provide the needed header in this field. In general the authorization is the Bearer token, this will be explained here.

To add headers to the HttpTransport we need to supply it as an array. First we create the header array.

$headers = [];

In this example we have the Bearer token stored in an array called data under the key authentication. We add this token to the header array under the key that must be called Authorization. Note the capitalization, this is required as well.

$headers['Authorization'] = 'Bearer '. $data['authentication'];

Now that we have the authorization header set we can use it to make the call to the external site.

$result = $http->get($url, $headers);

In the result we will have the answer from the external source. Now the HttpTransport has been used to call the external source with posting the credentials.

How to instantiate the HttpTransport see the linked article Making external calls using HttpFactory.

Related Articles