4
votes

I'm developing a Drupal 8 custom module. I have two fields (url and text html fields) in any node type. This is the features expected by the module:

The module will scrape the page of the "url field" and copy the html code to paste them in the "text html field" (this field is hidden in admin page). On every cron run this field content will be crushed.

Im using Drupal::httpClient (or Guzzle) to make my http request.

$client = \Drupal::httpClient();
$request = $client->get('https://github.com/codeafrica/github-africa');
$response = $request->getBody();

This my first Drupal 8 development module. To develop this module, do I need to create a custom Http Client to make my own Http Client? Where in my module repertories must I implement the functionalities of copying and pasting the html code. Can the controller do this?

This is the structure of my custom module:

custom_module.info.yml
custom_module.module
custom_module.routing.yml
src/Controller
src/Http  

I need guidance to start. Thanks for your help.

1

1 Answers

0
votes

I came across this question when I was working on something similar so I'm sharing the two ways I was able to get this to work.

I'm not sure if either of them is the best way :)

In my custom_module.module:

  1. Method 1

    $client = new \GuzzleHttp\Client();
    $url = 'yourURL';
    $res = $client->request('GET', $url);
    $res_body = $res->getBody();
    
  2. or Method 2

    use Guzzle\Http\Client;
    $client = new Client('yourURL');