Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue when make Guzzle Http call from one laravel app to another on same system

I'm facing a strange bug in laravel with Guzzle HTTP. I have 2 applications APP1 (act as client) and APP2 (act as server) in my localhost. The APP1 has to call APP2 by Guzzle HTTP to get data. When I call a URL in APP1, the action make a call to APP2 and return the response. But If we call through this way, I found that the APP2 uses the .env and database connections of APP1.

To confirm this I added the code in the action of the APP2.

return response()->json(['host' => DB::connection()->getConfig("host"), 'env_host' => env('DB_HOST')]);

if I call the APP2 url directly on the browser, it return the correct result:

{"host":"localhost","env_host":"localhost"}

But If I make a REST call over Guzzle HTTP from APP1 to APP2, it returns this response:

{"host":"localhostX","env_host":"localhostX"} //where localhostX is the value I added in .env file of APP1

This is the guzzle request code:

 client = new Client([ 'base_uri' => 'http://localhost/app2/', 'http_errors' => true, 'allow_redirect' => true ]);

        $response = $this->client->request('GET', $uri, []);

        $responseCode = $response->getStatusCode();
        $contentType  =  $response->getHeaderLine('content-type');
        $responseBody = $response->getBody()->getContents();
        dd($responseBody);

Can anybody have solution for this ? I think the guzzle makes REST which does not keep session.

I have already referred the question and answer Getting trouble when sending http request from one laravel project to another in same machine here. But I didn't see proper solution for the issue.

laravel version : 5.4 laracast link : https://laracasts.com/discuss/channels/laravel/laravel-guzzle-http-return-wrong-response

If I place the APP1 and APP2 under different servers (physically separated servers), it works fine as expected!!

like image 559
Hari Avatar asked Apr 21 '18 09:04

Hari


People also ask

Is guzzle using cURL?

Guzzle has historically only utilized cURL to send HTTP requests. cURL is an amazing HTTP client (arguably the best), and Guzzle will continue to use it by default when it is available. It is rare, but some developers don't have cURL installed on their systems or run into version specific issues.

Why we use guzzle in laravel?

Laravel provides an expressive, minimal API around the Guzzle HTTP client, allowing you to quickly make outgoing HTTP requests to communicate with other web applications. Laravel's wrapper around Guzzle is focused on its most common use cases and a wonderful developer experience.

How do you send a POST request on GuzzleHttp?

Sending Requests You can create a request and then send the request with the client when you're ready: use GuzzleHttp\Psr7\Request; $request = new Request('PUT', 'http://httpbin.org/put'); $response = $client->send($request, ['timeout' => 2]);

What is guzzle HTTP client?

Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services. Simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc...


1 Answers

I've got the same exact issue. After some research I have found that the problem is coming from 'phpdotenv'. See this link for more details: Laravel environment variables leaking between applications when they call each other through GuzzleHttp

And then I continue my reseach in the issue tracker of 'phpdotenv' and I found this: https://github.com/vlucas/phpdotenv/issues/219

php artisan config:cache is a good suggestion when using laravel; We also need another solution for separated phpdotenv; It is an annoying bugs. Waiting...@_@

For my case, I ended up running "php artisan config:cache" as a temporary solution.

[EDIT] I have found a better solution on this link, on the last post: https://laracasts.com/discuss/channels/general-discussion/env-not-reading-variables-sometimes

So instead of calling the env function anywhere on your application call the function config instead and add the variable in your config section.

like image 62
Eric Niaina Avatar answered Oct 29 '22 14:10

Eric Niaina