You are here

public function Api::purgeUrl in Fastly 8.3

Performs an actual purge request for the given URL.

Parameters

string $url: The full, valid URL to purge.

Return value

bool FALSE if purge failed or URL is invalid, TRUE is successful.

File

src/Api.php, line 341

Class

Api
Fastly API for Drupal.

Namespace

Drupal\fastly

Code

public function purgeUrl($url = '') {

  // Validate URL -- this could be improved.
  // $url needs to be URL encoded.
  // Need to make sure we can avoid double encoding.
  if (strpos($url, 'http') === FALSE && strpos($url, 'https') === FALSE) {
    return FALSE;
  }
  if (!UrlHelper::isValid($url, TRUE)) {
    return FALSE;
  }
  if (strpos($url, ' ') !== FALSE) {
    return FALSE;
  }
  if ($this->state
    ->getPurgeCredentialsState()) {
    try {

      // Use POST to purge/* to handle requests with http scheme securely.
      // See: https://docs.fastly.com/guides/purging/authenticating-api-purge-requests#purging-urls-with-an-api-token
      $response = $this
        ->query('purge/' . $url, [], 'POST');
      $result = $this
        ->json($response);
      if ($result->status === 'ok') {
        if ($this->purgeLogging) {
          $this->logger
            ->info('Successfully purged URL %url. Purge Method: %purge_method.', [
            '%url' => $url,
            '%purge_method' => $this->purgeMethod,
          ]);
        }
        return TRUE;
      }
      else {
        $this->logger
          ->critical('Unable to purge URL %url from Fastly. Purge Method: %purge_method.', [
          '%url' => $url,
          '%purge_method' => $this->purgeMethod,
        ]);
      }
    } catch (RequestException $e) {
      $this->logger
        ->critical($e
        ->getMessage());
    }
  }
  return FALSE;
}