Tập tành chọt Drupal 8/9 and Guzzle HTTP client library
4th Jul 2022Making a simple HTTP Get request in Drupal 8 or 9 is super easy thanks to the Guzzle library. This library has been added to Drupal core a long time ago, and it replaced the much less powerful drupal_http_request() function. Let's see how to get some data from the Star Wars API:
use GuzzleHttp\Client; use GuzzleHttp\Exception\RequestException; $client = new Client(); $people = []; try { $response = $client->get('https://swapi.dev/api/people'); $result = json_decode($response->getBody(), TRUE); foreach($result['results'] as $item) { $people[] = $item['name']; } } catch (RequestException $e) { // log exception }
Guzzle can also be accessed through the http_client service and the helper method:
$client = \Drupal::httpClient(); Sending a POST request is also simple and easy. public function sendOrder(OrderInterface $order) { try { $response = $this->httpClient->post($url, [ 'form_params' => $data, ]); $response_data = json_decode($response->getBody()->getContents(), TRUE); // do something with data } catch (RequestException $e) { // log exception } }
If you want to log the exception don't use $e->getMessage() because the error message will be truncated. Instead, use this:
$e->getResponse()->getBody()->getContents();
You can also use Guzzle to send files. To send an image file you can do something like this:
try { $client = new Client(['headers' => ['X-Auth-Secret-Token' => 'my-secret-token']]); $options = [ 'multipart' => [ [ 'Content-type' => 'multipart/form-data', 'name' => 'image', 'contents' => fopen($image->getFileUri(), 'r'), 'filename' => basename($image->getFileUri()), ], ], ]; $response = $client->post($api_base_url . 'api/v1/images', $options); } catch (\Exception $e) { // log exception }
In this case, the $image variable is an instance of Drupal\file\Entity\File class.
Add new comment