You are here

public function OEmbedProxyUrlController::request in Gutenberg 8

Same name and namespace in other branches
  1. 8.2 src/Controller/OEmbedProxyUrlController.php \Drupal\gutenberg\Controller\OEmbedProxyUrlController::request()

HTTP request.

Return value

bool|\Symfony\Component\HttpFoundation\JsonResponse The JSON response.

Throws

\GuzzleHttp\Exception\GuzzleException

1 string reference to 'OEmbedProxyUrlController::request'
gutenberg.routing.yml in ./gutenberg.routing.yml
gutenberg.routing.yml

File

src/Controller/OEmbedProxyUrlController.php, line 27

Class

OEmbedProxyUrlController
Class OEmbedProxyUrlController.

Namespace

Drupal\gutenberg\Controller

Code

public function request() {
  if (empty($_GET['url']) || !UrlHelper::isValid($_GET['url'], TRUE)) {
    throw new BadRequestHttpException("No valid URL was provided.");
  }
  try {
    $response = \Drupal::httpClient()
      ->request('GET', $_GET['url']);
    $data = $response
      ->getBody()
      ->getContents();
    $status = $response
      ->getStatusCode();
    $headers = $response
      ->getHeaders();
    $json = !empty($headers['Content-Type']) ? $headers['Content-Type'][0] == 'application/json' : FALSE;
    return new JsonResponse(json_decode($data));
  } catch (RequestException $e) {
    watchdog_exception('Gutenberg Editor', $e
      ->getMessage());
    throw new NotFoundException("The provided URL was not found.");
  }
}