¿Cuál es el equivalente de una llamada a drupal_http_request ()?

9

En Drupal 7 estoy usando el siguiente código.

$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;
}

¿Cuál es el código equivalente que debo usar en Drupal 8?

visabhishek
fuente

Respuestas:

13

Finalmente encuentro un código en https://api.drupal.org/api/drupal/core%21includes%21install.core.inc/8 (1375)

y está funcionando para mí :)

 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;
  }

Espero que esto ayude a alguien

visabhishek
fuente
2

Biblioteca de cliente HTTP agregada para reemplazar 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);
}
Adi
fuente
1
No funciona :( "El sitio web encontró un error inesperado. Vuelva a intentarlo más tarde.) El error de vigilancia es: Error fatal recuperable: el argumento 3 pasado a GuzzleHttp \ Client :: request () debe ser del tipo matriz, cadena dada, llamada /var/www/drupal8/vendor/guzzlehttp/guzzle/src/Client.php en la línea 87 y definido en GuzzleHttp \ Client-> request () (línea 126 de / var / www / drupal8 / vendor / guzzlehttp / guzzle / src /Client.php)
visabhishek
Lo arreglaron, pero sí, los registros de cambios son el primer lugar para verificar :)
wizonesolutions
El código anterior usa $ last_fetched var que no está definido en ningún lado y también en un lugar usa $ feed-> url y en otro $ feed-> uri
Marko Blazekovic
1

Esto está funcionando para mí, enviando un archivo XML con \ 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();

Espero que esto ayude.

Más información sobre Guzzle aquí: http://docs.guzzlephp.org/en/latest/index.html

Karim Boudjema
fuente