13

In Drupal 7 I am using the following code.

$url = 'testdomain/url';
$response = drupal_http_request($url, array('method' => 'POST', 'headers' => array('Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8')));
if ($response->code == "200") {
  $result = $response->data;
}

What is the equivalent code I should use on Drupal 8?

3 Answers 3

19

Finally i find a code in https://api.drupal.org/api/drupal/core%21includes%21install.core.inc/8 (1375)

and its working for me :)

 try {
    $response = \Drupal::httpClient()->get($uri, array('headers' => array('Accept' => 'text/plain')));
    $data = (string) $response->getBody();
    if (empty($data)) {
      return FALSE;
    }
  }
  catch (RequestException $e) {
    return FALSE;
  }

Hope this will help for some one

1
  • 1
    No surprise, but this works for Drupal 9 as well.
    – bkudrle
    Commented Dec 25, 2021 at 23:27
2

HTTP client library added to replace drupal_http_request()

$client = \Drupal::httpClient();
$request = $client->createRequest('GET', $feed->url);
$request->addHeader('If-Modified-Since', gmdate(DATE_RFC1123, $last_fetched));

try {
  $response = $client->get($feed->uri, [
    'headers' => [
      'If-Modified-Since' => gmdate(DATE_RFC1123, $last_fetched),
    ],
  ]);
  // Expected result.
  // getBody() returns an instance of Psr\Http\Message\StreamInterface.
  // @see http://docs.guzzlephp.org/en/latest/psr7.html#body
  $data = $response->getBody();
}
catch (RequestException $e) {
  watchdog_exception('my_module', $e);
}
5
  • 1
    Not Working :( "The website encountered an unexpected error. Please try again later.) Watchdog error is : Recoverable fatal error: Argument 3 passed to GuzzleHttp\Client::request() must be of the type array, string given, called in /var/www/drupal8/vendor/guzzlehttp/guzzle/src/Client.php on line 87 and defined in GuzzleHttp\Client->request() (line 126 of /var/www/drupal8/vendor/guzzlehttp/guzzle/src/Client.php) Commented Jan 22, 2016 at 6:28
  • drupal.org/node/1862446#comment-10640294 Commented Jan 22, 2016 at 6:34
  • They fixed it, but yeah, change records are the first place to check :) Commented Jan 23, 2016 at 7:16
  • The code above uses $last_fetched var which is not defined anywhere and also in one place uses $feed->url and in onther $feed->uri
    – Blissful
    Commented Sep 14, 2018 at 11:29
  • The error comes up as the RequestException class is not defined. Add use GuzzleHttp\Exception\RequestException; Commented Jul 27, 2020 at 19:00
1

This is working for me, sending a XML file with \Drupal::httpClient() POST

$endpoint  = 'http://example.com/something';
$xml = '<>'; // You're XML here.

// Make the request.
$options = [
  'connect_timeout' => 30,
  'debug' => true,
  'headers' => array(
    'Content-Type' => 'text/xml',
  ),
  'body' => $xml,
  'verify'=>true,
];

try {
  $client = \Drupal::httpClient();
  $request = $client->request('POST',$endpoint,$options);

}
catch (RequestException $e){
  // Log the error.
  watchdog_exception('custom_modulename', $e);
}

$responseStatus = $request->getStatusCode();
$responseXml = $request->getBody()->getContents();

Hope this helps.

More info about Guzzle here: http://docs.guzzlephp.org/en/latest/index.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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