Jump to content


Popular Content

Showing content with the highest reputation on 01/03/2019 in all areas

  1. 1 point
    Turns out this is even easier than I thought. My configuration has two http servers on the same machine where one is mainwebsite.com and it makes curl requests to the other which is api.mainwebsite.com . Now, I am just sending the Slim Request to Guzzle. Originally, I sent Slim's Request with its original host (mainwebsite.com), and my $_SESSION variable kept on getting deleted. I am assuming some sort of web browser or PHP security feature? Any thoughts? Why wasn't the base_uri added to \GuzzleHttp\Client's constructor not being applied? Regardless, I was able to change the host as shown below and all now works. To use less code, I am using a loop to apply the headers to the response. Think I should hard code it like $slimResponse->withHeader('header1', $guzzleResponse->getHeader('header1'))->addMoreHeaders()->thenAddStatusAndBody()? This (I think) will eliminate needing to clone all the responses, however, will will require me to have code for each Content-Type to determine which headers are returned. But maybe that is good and I shouldn't blacklist headers as I am doing but whitelist them? If this approach is acceptable, what other headers should I blacklist? Thanks public function proxy(\Slim\Http\Request $slimRequest, \Slim\Http\Response $slimResponse):\Slim\Http\Response { //Forwards Slim Request to another server and returns the updated Slim Response. $slimRequest=$slimRequest->withUri($slimRequest->getUri()->withHost($this->getHost(false))); //Change slim's host to API server! try { $httpClient = new \GuzzleHttp\Client(['base_uri' => "https://api.mainwebsite.com"]); //Will use injection, and just shown this way ease $guzzleResponse=$httpClient->send($slimRequest); $excludedHeaders=['Date', 'Server', 'X-Powered-By', 'Access-Control-Allow-Origin', 'Access-Control-Allow-Methods', 'Access-Control-Allow-Headers']; $headerArrays=array_diff_key($guzzleResponse->getHeaders(), array_flip($excludedHeaders)); foreach($headerArrays as $headerName=>$headers) { foreach($headers as $headerValue) { $slimResponse=$slimResponse->withHeader($headerName, $headerValue); } } return $slimResponse->withStatus($guzzleResponse->getStatusCode())->withBody($guzzleResponse->getBody()); } catch (\GuzzleHttp\Exception\RequestException $e) { if ($e->hasResponse()) { $guzzleResponse=$e->getResponse(); return $slimResponse->withStatus($guzzleResponse->getStatusCode())->withBody($guzzleResponse->getBody()); } else { return $slimResponse->withStatus(500)->write(json_encode(['message'=>'RequestException without response: '.$e->getMessage()])); } } }
This leaderboard is set to New York/GMT-04:00
  • Newsletter

    Want to keep up to date with all our latest news and information?

    Sign Up

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.