-3

I have set up a very simple web service in another server, using plain PHP, which receives data as GET, does a database search, and retrieves a JSON string. From the server where Drupal is installed, I could get the data using CURL.

Now I need to do the same in Drupal, using username as parameter. How can I achieve this?

All I can find relates to creating web services in Drupal rather than consuming them.

0

OK, after a lot of research and guessing I think I've solved it, this is my solution in case anyone needs it

<?php

namespace Drupal\mymodule\Controller;

use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use GuzzleHttp\Client;

class MyModuleController extends ControllerBase {
  protected $httpClient;

public function __construct(Client $http_client) {
  $this->httpClient = $http_client;
}

public static function create(ContainerInterface $container) {
  return new static(
    $container->get('http_client')
  );
}

public function content() {
  $user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
  $name = $user->get('name')->value;
  $myurl = 'http://(remote url)?doc='.$name;

  $request = $this->httpClient->request('GET',$myurl);

  if ($request->getStatusCode() == 200) {
     $datos = $request->getBody()->getContents();
     return ['#markup' => $datos,];
  }
}
}
?>

This only shows the answer as a string, now I'll have to parse it, etc, and add some security and validation. Opinions are welcome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.