PHP: Remote function Call and return result?

I am not very good at PHP. I want to know how to communicate between two web servers. To enable (from the 1st server) run the function (request) on the remote server. And return the result to the 1st server.

Actually the topic will be:
Web Server (1) ----------------> Web Server (2) ---------------> Database Server
Web Server (1) <---------------- Web Server (2) <--------------- Database Server

Query Function() will only reside on Web Server (2) . Then I need to run this Query Function() remotely from Web Server (1) .

What is he calling? And is it possible?

+6
source share
3 answers

Yes.

A good way I can think of would be to send a request to the 2nd server via the url. In the GET (or POST) parameters, specify which method you want to call, and (for security) some hash that changes over time. The hash inside is there to ensure that the third party cannot execute the function arbitrarily on the 2nd server.

To send a request, you can use cURL:

 function get_url($request_url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $request_url); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); curl_close($ch); return $response; } 

Sends a GET request. Then you can use:

 $request_url = 'http://second-server-address/listening_page.php?function=somefunction&securityhash=HASH'; $response = get_url($request_url); 

On your second server, set up listening_page.php (with whatever file name you like, of course), which checks for GET requests and checks for the integrity of the request (i.e. hash, valid and valid parameters).

+11
source

You can do this using the API. create a page on a second server that accepts the variables and communicates with the server using these vars (depending on what you need). and the standard answer from this page should be either JSON or XML. then read this from server 1, requesting this file and receiving a response from the second server.

* NOTE, if this is a private file, make sure you use the authentication method to prevent users from accessing the file

0
source

What you are trying to do is definitely possible. You will need to configure some kind of api so that the server can make a request to server 2.

I suggest you read the SOAP and REST api

http://www.netmagazine.com/tutorials/make-your-own-soap-api

Typically, you use something like CURL to contact server 2 from server 1.

Google is twisting, and you should get a quick idea.

It won't be easy to give you a complete solution, so I hope this push in the right direction is helpful.

0
source

Source: https://habr.com/ru/post/897559/


All Articles