Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cverster's avatar

Getting a 400 when requesting API on remote server but 200 when requesting locally

Hi all

I'm seeing some strange behaviour on my remote server that I'm not sure how to debug. I execute the following code to make an API request:

$my_field_value= '123';
$key = env('API_KEY');
$username = env('API_USERNAME');

$model = MyModel::where('my_field', $my_field_value)->first();

$meta_url = 'https://[url_to_api].com/path/thats/relevant/api?username=' . $username
            .'&key=' . $key;

$meta_response = Http::get($meta_url);
dd($meta_response);

When I execute this code locally, I get a 200, but if I execute exactly the same code on my remote server I get a 400. What could be causing this issue, and how can I go about debugging it?

0 likes
8 replies
cverster's avatar

@Sinnbeck Thanks Sinbeck. I'll change that now. Strange thing is when I print the $meta_url, I still got the environmental variable included?

cverster's avatar

@Sinnbeck In production. Thanks for the tip, I've moved everything to a config file that references .env, but unfortunately I'm still getting the same 400 error (though if I check the http response, the url is correct and I can execute it in my browser with the desired response, a 200).

Sinnbeck's avatar

@verster.cornel@gmail.com Is there any way you can ask the people who has the api why they see the request as malformed then? I assume you can copy paste it into a browser and it will just work there?

cverster's avatar

@Sinnbeck Yeah I'll do that, thanks! I just wanted to make sure I'm not doing something obvious wrong that would be different between my local and prod environments. Could running composer update on prod (which I did to get rid of a Log depreciation error) maybe have caused a change in how my Http client operates?

Sinnbeck's avatar

@verster.cornel@gmail.com That is why you have a composer.lock file. When you deploy, you run composer install, to install exactly the same versions as in development. That way you dont get any surprices.

But the code looks fine, except for those 2 env calls :)

1 like
Snapey's avatar

try to curl that url from the server command line

Please or to participate in this conversation.