How To Integrate A 3rd Party API In Drupal

Posted By : Nitish Devrani | 23-Dec-2020

Introduction

If you have integrated 3rd party APIs in PHP, It's the same in Drupal as well.

use Curl for the task because it does a completely smooth PHP oriented task. You no need to switch over javascript for the API call work.

It's solely dependent on PHP.

 

Requirements

  1. Basic knowledge of PHP.
  2. Apache Server for PHP processing.
  3. Drupal Installed on the Machine. 

Let's Get Started:

First of all, in drupal, we need an area to show this API response. So let's create a new page.

To create a new blank page in drupal use a drupal hook:- modulename_hook. Just like the below image.

 

blank-page-creation-code

After this above example, you will get the blank page at frontend.

blank-page

 

Also Read: Optimize Images in WordPress

 

Now as we can see our blank page. Let's get a sample API for demonstration.

I'm using API from here.https://reqres.in/

Now as we have the sample API now we need to use it in our Drupal Blank Page.

Let's create a function where we use and print all our data which is called by our drupal hook.

function call

 

Now let's create an API call method. I'm using CURL here because its best for PHP and doesn't require any other dependencies.

curl

 

 

Now, as you can see in the last line of this above image. We are printing the API response data.

 

Also Read: Understanding The Concept of Functional Programming

 

filled-api-response-page

 

This is the response you will see after API call using curl from Drupal.

Then use it in your OWN way.

For example, we want the user profile picture. for that, we need to go inside the response Array.

So the code will look like this.

code

After that save your file and refresh your page. You will see this.

blank-pagenow image

We are in the final stage now. We got our image URL. Now just use the image tag of HTML and display this image at your blank page.

 

Also Read: Top 5 Software Tools a Web Developer Should Have.

 

final-code

And what we'll get out of this is here.

output

That's it.

Summary

If you want to use any 3rd Party API in your Drupal site. Just use CURL for the fast experience to develop and use postman for testing. Then Create a blank page to try and catch the code bugs. After you'll get your final results from that page, copy the code and use it anywhere you want for the task.

 

We are a 360-degree software development company that provides complete web and mobile app development solutions for varied project requirements. Our end-to-end SaaS app development services address your mission-critical project requirements through scalable, responsive, and feature-rich software applications that are easy to scale. We carefully analyze your project requirements and formulate effective strategies to build enterprise-grade web and mobile applications for multiple platforms. For more info, contact us at [email protected]

About Author

Author Image
Nitish Devrani

Nitish is a passionate Full Stack Web Developer and always available to Learn and Teach new WEB Technologies. He is a Gadget geek and always wanted to acquire latest Gadgets.

Request for Proposal

Name is required

Comment is required

Sending message..