There are many ways to communicate with external web services but the most common method from drupal is using S3 bucket from AWS. There is already a module for that which is S3 File System from durpal.org site . But we want more customized code based request functionality also known as “Custom Module”. The reason why we want to create a custom module is we can give the client what exactly they need and also for security purposes.
This example show us how to create custom module specifically to create request to external server. There are many types on how to approach the request function . The most common is to use the hooks . These hooks will run at the middle of runtime depends on where you insert the function.
For the Drupal Service , we use Drupal::httpClient() which is core library from Drupal.
Insert the code inside my_custom_module.module.
function my_custom_module_form_alter(&$form, FormStateInterface $form_state, $form_id){
$url = 'https://drupal.acret.jp/sites/default/files/styles/large/public/2020-12/Command-Line-1280x720.jpg';
$client = \Drupal::httpClient();
$response = $client->request('GET',$url)->getBody()->getContents();
ksm($response);
}
ksm function will give you the displayed result of the second image shown.This is from Devel kint module. It means , This will trigger the request when we access any page which has a form element and we have successfully executed the request from.
The example above is we are trying to request the image file from another server.It means get resources from another server. It could also be other types of file like JSON,XML,txt etc. These features can extend beyond the ordinary fetch of an image . Because of this , we can also use the RestAPI web service feature .
This Example Show us simple RestAPI . This method will access the custom path to Drupal server then it will return JSON content which Drupal server fetches from external Server.
- Create a route file which Drupal recognized the path and registered on the system as a valid path.
Inside my_custom_module.routing.yml
Copy Paste
custom_requests.controller:
path: '/api/time'
defaults:
_controller: '\Drupal\my_custom_module\Controller\TimeController::run'
_title: 'Rest Api Sample'
requirements:
_access: 'TRUE'
2. Create controller file to instruct the Drupal what kind of response will send to client side.
Create TimeController.php under /modules/custom/my_custom_module/Controller
<?php
namespace Drupal\my_custom_module\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
class TimeController extends ControllerBase {
public function run() {
$url = 'http://worldclockapi.com/api/json/utc/now';
$client = \Drupal::httpClient();
$response = $client->request('GET',$url)->getBody()->getContents();
$jsonResponse = new JsonResponse(json_decode($response,TRUE));
return $jsonResponse;
}
}
3. Access your custom path into browser example. localhost//api/time .
This JSON data was fetched from http://worldclockapi.com/api/json/utc/now server which is external server.
In Conclusion . Drupal request method is very versatile and can be made in many forms such as fetching resources like images etc specially dealing with authentication to fully access the data like RestAPI service..