Proxy\Proxy::forward PHP Method

forward() public method

public forward ( Proxy\Http\Request $request, $url )
$request Proxy\Http\Request
    public function forward(Request $request, $url)
    {
        // change request URL
        $request->setUrl($url);
        // prepare request and response objects
        $this->request = $request;
        $this->response = new Response();
        $options = array(CURLOPT_CONNECTTIMEOUT => 10, CURLOPT_TIMEOUT => 0, CURLOPT_RETURNTRANSFER => false, CURLOPT_HEADER => false, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_FOLLOWLOCATION => false, CURLOPT_AUTOREFERER => false);
        // this is probably a good place to add custom curl options that way other critical options below would overwrite that
        $config_options = Config::get('curl', array());
        $options = array_merge_custom($options, $config_options);
        $options[CURLOPT_HEADERFUNCTION] = array($this, 'header_callback');
        $options[CURLOPT_WRITEFUNCTION] = array($this, 'write_callback');
        // Notify any listeners that the request is ready to be sent, and this is your last chance to make any modifications.
        $this->dispatcher->dispatch('request.before_send', new ProxyEvent(array('request' => $this->request, 'response' => $this->response)));
        // We may not even need to send this request if response is already available somewhere (CachePlugin)
        if ($this->request->params->has('request.complete')) {
            // do nothing?
        } else {
            // any plugin might have changed our URL by this point
            $options[CURLOPT_URL] = $this->request->getUri();
            // fill in the rest of cURL options
            $options[CURLOPT_HTTPHEADER] = explode("\r\n", $this->request->getRawHeaders());
            $options[CURLOPT_CUSTOMREQUEST] = $this->request->getMethod();
            $options[CURLOPT_POSTFIELDS] = $this->request->getRawBody();
            $ch = curl_init();
            curl_setopt_array($ch, $options);
            // fetch the status - if exception if throw any at callbacks, then the error will be supressed
            $result = @curl_exec($ch);
            // there must have been an error if at this point
            if (!$result) {
                $error = sprintf('(%d) %s', curl_errno($ch), curl_error($ch));
                throw new \Exception($error);
            }
            // we have output waiting in the buffer?
            $this->response->setContent($this->output_buffer);
            // saves memory I would assume?
            $this->output_buffer = null;
        }
        $this->dispatcher->dispatch('request.complete', new ProxyEvent(array('request' => $this->request, 'response' => $this->response)));
        return $this->response;
    }

Usage Example

Esempio n. 1
0
 /**
  * action for routing all requests directly to the third party API
  *
  * @param Request $request request
  *
  * @return \Psr\Http\Message\ResponseInterface|Response
  */
 public function proxyAction(Request $request)
 {
     $api = $this->decideApiAndEndpoint($request->getUri());
     $this->registerProxySources();
     $url = $this->apiLoader->getEndpoint($api['endpoint'], true);
     if (parse_url($url, PHP_URL_SCHEME) === false) {
         $scheme = $request->getScheme();
         $url = $scheme . '://' . $url;
     }
     $response = null;
     try {
         $newRequest = Request::create($url, $request->getMethod(), array(), array(), array(), array(), $request->getContent(false));
         $newRequest->headers->add($request->headers->all());
         $newRequest = $this->transformationHandler->transformRequest($api['apiName'], $api['endpoint'], $request, $newRequest);
         $psrRequest = $this->diactorosFactory->createRequest($newRequest);
         $psrRequest = $psrRequest->withUri($psrRequest->getUri()->withPort(parse_url($url, PHP_URL_PORT)));
         $psrResponse = $this->proxy->forward($psrRequest)->to($this->getHostWithScheme($url));
         $response = $this->httpFoundationFactory->createResponse($psrResponse);
         $this->transformationHandler->transformResponse($api['apiName'], $api['endpoint'], $response, clone $response);
     } catch (ClientException $e) {
         $response = $e->getResponse();
     } catch (ServerException $serverException) {
         $response = $serverException->getResponse();
     }
     return $response;
 }
All Usage Examples Of Proxy\Proxy::forward