0

Prefacing this by saying that I am definitely new to Wordpress, so there may be some sort of configuration that I do not know of causing this (I am hoping it is this simple of a fix)

Recently I've taken over a decently sized Wordpress environment and am working on getting up to speed with all the quirks that come with Wordpress.

For testing purposes, what I have done is alter my hosts file to redirect all requests to my production environment, to hit local instead so that I can test local changes I've made to an endpoint that is currently running in production.

example:

127.0.0.1 api.mywebsite.com

So when testing a piece of code that calls this endpoint I have updated, I am expecting it to reroute to the local version.

The issue is that even with this host file change, for some reason I am always getting back the current version of the endpoint (the one in prod right now) and getting back what is effectively the "old" response from the endpoint.

It get's stranger though when in my code I manually change it to specifically hit localhost:

$data = wp_remote_get('http://localhost/site/data.json'); 
print_r($data);

(I have also tried specifically 127.0.0.1)

The output from this print statement only gives me back the headers of the response, and then the "Body" is completely empty.

This is very strange though because if I just run a cURL to the route, or just hitting it in a browser locally I am able to get the valid response back every single time, so it is definitely not an issue with the logic of the code being run in the endpoint itself.

I have tried changing the way in which I am running the GET to that endpoint, by using the code below, but the exact same situation occurs, so it does not seem like it is a problem with the way in which I am trying to call this endpoint.

$apiRequest = new WP_Http;

$data = $apiRequest->request(
    'http://localhost/site/data.json',
    array('decompress' => false)
);

Does anyone know if there are some sort of Wordpress project settings that could be causing this? Such as overriding my hosts file change that redirect all prod traffic to local?

I have never run into a situation like this before, and my attempts at debugging it thus far have been completely useless.

Just to reiterate my situation here, what I am trying to do is be able to test the local changes I've made to an production endpoint by hitting the prod environment and having my hosts file redirect me to the local version, but it seems like my Wordpress configuration (or something else) is always trying to re route me back to what the current prod version of the endpoint is, thus getting a response that is not updated.

Then if I try to specifically tell in my code to hit the local version of the endpoint, I am getting back NO response in the "body" at all. (example below):

Array ( [headers] => Requests_Utility_CaseInsensitiveDictionary Object ( [data:protected] => Array ( [server] => nginx/1.10.2 [date] => Fri, 17 Mar 2017 17:31:37 GMT [content-type] => text/html; charset=UTF-8 [x-powered-by] => PHP/5.5.9-1ubuntu4.20 [set-cookie] => Array ( [0] => PHPSESSID=f42o8ubvk0ohr2mfjogmvmm5p4; path=/ [1] => wfvt_3956933299=58cc1d7825ee1; expires=Fri, 17-Mar-2017 18:01:36 GMT; Max-Age=1800; path=/; httponly ) [pragma] => no-cache [expires] => Wed, 11 Jan 1984 05:00:00 GMT [cache-control] => no-cache, must-revalidate, max-age=0 [link] => ; rel="https://api.w.org/" [content-encoding] => gzip ) ) [body] => 

Any help is greatly appreciated!

parchambeau
  • 1,141
  • 9
  • 34
  • 56
  • It's not mentioned above, but I assume you've added the virtual host to your local web server? – Greg Burkett Mar 17 '17 at 21:01
  • No I have not actually, what would be the advantage here? Sorry for ignorance – parchambeau Mar 20 '17 at 13:43
  • You've got the domain name pointed to your localhost IP address, but you'd also need to tell your localhost webserver to expect that traffic at that domain name and where to point it in your root folder. The method for doing this varies greatly depending on your web server software, but if you provide some info we can probably help out! – Greg Burkett Mar 20 '17 at 15:18
  • So after further investigation it seems like the problem is that I have a local api hosted on my machine using apache as webserver, but the code that is trying to invoke this api is being run on a Vagrant box with nginx, so when the php on that Vagrant box goes to hit "localhost" routes, it has no idea that the localhost I am trying to have it hit is the one hosted on my local machine. Any idea how I would be able to tell it to hit that one instead? Thanks! – parchambeau Mar 20 '17 at 15:25
  • Can't edit the above comment for some reason, but meant to add that the Vagrant box thinks that it is supposed to be hitting "localhost" on itself, but in reality I need it to be hitting the "localhost" that is on my machine. – parchambeau Mar 20 '17 at 15:36
  • Hmm.. this isn't my area of expertise, unfortunately, but it may be worth editing your question or starting a new one that lists that problem as the main issue. You'd probably get some one to come along with a response pretty shortly! – Greg Burkett Mar 20 '17 at 16:17
  • 1
    Found an answer here: https://stackoverflow.com/questions/19933550/how-to-connect-with-host-postgresql-from-vagrant-virtualbox-machine I have no idea why "10.0.2.2" works, but it does. Thanks for your help! – parchambeau Mar 20 '17 at 16:30

0 Answers0