Model/Api.php
private function _purge($uri, $type, $method = \Zend_Http_Client::POST, $payload = null)
    {

        if ($method == 'PURGE') {
            // create purge token
            $expiration   = time() + self::PURGE_TOKEN_LIFETIME;

            $zendUri = \Zend_Uri::factory($uri);
            $path = $zendUri->getPath();
            $stringToSign = $path . $expiration;
            $signature    = hash_hmac('sha1', $stringToSign, $this->config->getServiceId());
            $token        = $expiration . '_' . urlencode($signature);
            $headers = [
                self::FASTLY_HEADER_TOKEN . ': ' . $token
            ];
        } else {
            // set headers
            $headers = [
                self::FASTLY_HEADER_AUTH  . ': ' . $this->config->getApiKey()
            ];
        }

        // soft purge if needed
        if ($this->config->canUseSoftPurge()) {
            array_push(
                $headers,
                self::FASTLY_HEADER_SOFT_PURGE . ': 1'
            );
        }
        $result['status'] = true;
        try {
            $client = $this->curlFactory->create();
            $client->setConfig(['timeout' => self::PURGE_TIMEOUT]);
            if ($method == 'PURGE') {
                $client->addOption(CURLOPT_CUSTOMREQUEST, 'PURGE');
            }
            $client->write($method, $uri, '1.1', $headers, $payload);
            $responseBody = $client->read();
            $responseCode = \Zend_Http_Response::extractCode($responseBody);
            $responseMessage = \Zend_Http_Response::extractMessage($responseBody);
            $client->close();

            // check response
            if ($responseCode == '429') {
                throw new LocalizedException(__($responseMessage));
            } elseif ($responseCode != '200') {
                throw new LocalizedException(__($responseCode . ': ' . $responseMessage));
            }
        } catch (\Exception $e) {
            $this->logger->critical($e->getMessage(), $uri);
            $result['status'] = false;
            $result['msg'] = $e->getMessage();
        }

        if (empty($type)) {
            return $result;
        }

        if ($this->config->areWebHooksEnabled() && $this->config->canPublishPurgeChanges()) {
            $this->sendWebHook('*initiated ' . $type .'*');

            if ($this->config->canPublishPurgeDebugBacktrace() == false) {
                return $result;
            }

            $this->stackTrace($type);
        }

        return $result;
    }
private function _purge($uri, $type, $method = \Zend_Http_Client::POST, $payload = null)
    {

        if ($method == 'PURGE') {
            // create purge token
            $expiration   = time() + self::PURGE_TOKEN_LIFETIME;

            $zendUri = \Zend_Uri::factory($uri);
            $path = $zendUri->getPath();
            $stringToSign = $path . $expiration;
            $signature    = hash_hmac('sha1', $stringToSign, $this->config->getServiceId());
            $token        = $expiration . '_' . urlencode($signature);
            $headers = [
                self::FASTLY_HEADER_TOKEN . ': ' . $token
            ];
        } else {
            // set headers
            $headers = [
                self::FASTLY_HEADER_AUTH  . ': ' . $this->config->getApiKey()
            ];
        }

        // soft purge if needed
        if ($this->config->canUseSoftPurge()) {
            array_push(
                $headers,
                self::FASTLY_HEADER_SOFT_PURGE . ': 1'
            );
        }
        $result['status'] = true;
        try {
            $client = $this->curlFactory->create();
            $client->setConfig(['timeout' => self::PURGE_TIMEOUT]);
            if ($method == 'PURGE') {
                $client->addOption(CURLOPT_CUSTOMREQUEST, 'PURGE');
            }
            $client->write($method, $uri, '1.1', $headers, $payload);
            $responseBody = $client->read();
            $responseCode = \Zend_Http_Response::extractCode($responseBody);
            $responseMessage = \Zend_Http_Response::extractMessage($responseBody);
            $client->close();

            // check response
            if ($responseCode == '429') {
                throw new LocalizedException(__($responseMessage));
            } elseif ($responseCode != '200') {
                throw new LocalizedException(__($responseCode . ': ' . $responseMessage));
            }
        } catch (\Exception $e) {
            $this->logger->critical($e->getMessage(), $uri);
            $result['status'] = false;
            $result['msg'] = $e->getMessage();
        }

        if (empty($type)) {
            return $result;
        }

        if ($this->config->areWebHooksEnabled() && $this->config->canPublishPurgeChanges()) {
            $this->sendWebHook('*initiated ' . $type .'*');

            if ($this->config->canPublishPurgeDebugBacktrace() == false) {
                return $result;
            }

            $this->stackTrace($type);
        }

        return $result;
    }
private function _purge ( $ uri , $ type , $ method = \ Zend_Http_Client :: POST , $ payload = null ) { if ( $ method == 'PURGE' ) { // create purge token $ expiration = time ( ) + self :: PURGE_TOKEN_LIFETIME ; $ zendUri = \ Zend_Uri :: factory ( $ uri ) ; $ path = $ zendUri -> getPath ( ) ; $ stringToSign = $ path . $ expiration ; $ signature = hash_hmac ( 'sha1' , $ stringToSign , $ this -> config -> getServiceId ( ) ) ; $ token = $ expiration . '_' . urlencode ( $ signature ) ; $ headers = [ self :: FASTLY_HEADER_TOKEN . ': ' . $ token ] ; } else { // set headers $ headers = [ self :: FASTLY_HEADER_AUTH . ': ' . $ this -> config -> getApiKey ( ) ] ; } // soft purge if needed if ( $ this -> config -> canUseSoftPurge ( ) ) { array_push ( $ headers , self :: FASTLY_HEADER_SOFT_PURGE . ': 1' ) ; } $ result [ 'status' ] = true ; try { $ client = $ this -> curlFactory -> create ( ) ; $ client -> setConfig ( [ 'timeout' => self :: PURGE_TIMEOUT ] ) ; if ( $ method == 'PURGE' ) { $ client -> addOption ( CURLOPT_CUSTOMREQUEST , 'PURGE' ) ; } $ client -> write ( $ method , $ uri , '1.1' , $ headers , $ payload ) ; $ responseBody = $ client -> read ( ) ; $ responseCode = \ Zend_Http_Response :: extractCode ( $ responseBody ) ; $ responseMessage = \ Zend_Http_Response :: extractMessage ( $ responseBody ) ; $ client -> close ( ) ; // check response if ( $ responseCode == '429' ) { throw new LocalizedException ( __ ( $ responseMessage ) ) ; } elseif ( $ responseCode != '200' ) { throw new LocalizedException ( __ ( $ responseCode . ': ' . $ responseMessage ) ) ; } } catch ( \ Exception $ e ) { $ this -> logger -> critical ( $ e -> getMessage ( ) , $ uri ) ; $ result [ 'status' ] = false ; $ result [ 'msg' ] = $ e -> getMessage ( ) ; } if ( empty ( $ type ) ) { return $ result ; } if ( $ this -> config -> areWebHooksEnabled ( ) && $ this -> config -> canPublishPurgeChanges ( ) ) { $ this -> sendWebHook ( '*initiated ' . $ type . '*' ) ; if ( $ this -> config -> canPublishPurgeDebugBacktrace ( ) == false ) { return $ result ; } $ this -> stackTrace ( $ type ) ; } return $ result ; }
Send purge request via Fastly API

@param $uri
@param $type
@param string $method
@param null $payload
@return \Magento\Framework\Controller\Result\Json
@throws \Zend_Uri_Exception
Send purge request via Fastly API