How To Integrate A 3rd Party API In Drupal
Posted By : Nitish Devrani | 23-Dec-2020
If you have integrated 3rd party APIs in PHP, It's the same in Drupal as well.
It's solely dependent on PHP.
- Basic knowledge of PHP.
- Apache Server for PHP processing.
- 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.
After this above example, you will get the blank page at frontend.
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.
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.
Now, as you can see in the last line of this above image. We are printing the API response data.
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.
After that save your file and refresh your page. You will see this.
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.
And what we'll get out of this is here.
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 firstname.lastname@example.org.