Can WordPress pass through a call to an external API and return the result directly to the client?

The question:

I have a wordpress site that will be replacing an old legacy site, however the old legacy site includes a SOAP API that I won’t be able to re-implement in wordpress.

Is it possible for the wordpress site to accept calls to these API route, call the API itself, and then return the result to the caller as if the original API had responded? (i.e. without getting a wordpress page or some other wordpress artefact in the response?)

For example:

A call to:

www.mydomain.com/api/getcount/

would want to trigger a call to:

www.mylegacyendpoint.net/api/getcount

and return the result so that when the DNS changes and the new wordpress site goes live on the domain (replacing the old legacy site on that domain) the API calls are unaffected.

I’m not very familiar with wordpress as a platform and so I’m not sure if this is something that can be handled within wordpress or something that would need to be handed in the hosting environment

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

I think most of the possibilities using something like a rewrite rule to execute a some PHP to forward and return the request at an action very early in the WordPress request’s lifecycle would have a number of drawbacks.

Other alternatives would be in using a rewrite rule to direct the request to a custom PHP AJAX handler or REST API endpoint or controller and forward on the request from there – both of these mechanisms are designed to load a smaller portion of WordPress’s core. But in all of the above options, the overhead of loading WordPress, parsing the original request, and building a new one for each proxied request is still significant in my opinion. They also introduces an additional point of failure. But they’re all certainly possible, and you can definitely return responses without the extra headers which WordPress typically adds.

It would be far easier and more efficient to perform this forwarding operation at the webserver level (or reverse proxy/load balancer prior to the webserver, if your architecture uses such a mechanism), using Apache’s ProxyPass directive as facilitated by mod_proxy, or Nginx’s equivalent directives.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment