7

I'm not very expert to PHP. I want to know how to communicate between 2 web servers. For clearance, (from 1st Server) run a function (querying) on remote server. And return the result to 1st server.

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

Query Function() will be only located on Web Server (2). Then i need to run that query function() remotely from Web Server (1).

What is it call? And Is it possible?

夏期劇場
  • 17,821
  • 44
  • 135
  • 217
  • Have you had a look at [these questions](http://stackoverflow.com/search?q=php+rpc)? – Felix Kling Sep 18 '11 at 09:25
  • RPC? Is this case is about RPC, you mean? – 夏期劇場 Sep 18 '11 at 09:28
  • 1
    RPC stands for *Remote Procedure Call* and it seems to be what you want to do. It is a general protocol (or concept). Variations are RMI (Java), XML-RPC, JSON-RPC.... there is a lot to choose from. You could even create your own. But that doesn't mean that this is the best way in your situation. [Maybe REST would be better](http://stackoverflow.com/questions/1098473/rest-vs-rpc). – Felix Kling Sep 18 '11 at 09:31

3 Answers3

12

Yes.

A nice way I can think of doing would be to send a request to the 2nd server via a URL. In the GET (or POST) parameters, specify which method you'd like to call, and (for security) some sort of hash that changes with time. The hash in there to ensure no third-party can run the function arbitrarily on the 2nd server.

To send the request, you could 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;
}

This sends a GET request. You can then 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 the listening_page.php (with whatever filename you like, of course) that checks for GET requests and verifies the integrity of the request (i.e. the hash, correct & valid params).

Alex
  • 9,313
  • 1
  • 39
  • 44
  • Woah! Is this can be done that simply? So i can make any function() calls directly and get the return, as simply as in same local function..? – 夏期劇場 Sep 18 '11 at 09:43
  • Yes. You'll need to 'expose' the functions that you want to be called though. (i.e. server 2 gets the GET request, then *it* executes the requested function, then passes the response back to server 1, so server 1 isn't *directly* calling the remote function but it has a similar effect). – Alex Sep 18 '11 at 09:48
  • I've tested and it works buddy :D So simple. Is it the correct solution of these? I mean, others are saying to use REST API, SOAP etc. Why please.. – 夏期劇場 Sep 18 '11 at 10:08
  • 2
    What you've implemented is, in essence, an API. Server 1 is requesting information from the API of server 2. REST and SOAP are ways of passing data around. They implement certain conventions for requesting data, and structure their return data in a meaningful and consistent manner. If you're going to to be requesting a lot of data from server 2, you should consider putting more thought in to your API. There are lots of resources about for such a task (Google 'building an api' or similar). – Alex Sep 18 '11 at 10:12
  • Ah i see now :) REST and SOAP are more suitable for COMPLEX & HUGE amount of data transfers. Ok so. My condition will be ok with your one coz of possibly few data input and return. But final question (please). Is it SAFE for multi loads? I mean, can i use your way if there are multiple concurrent user calls to this 1 file (server 2 API File) ? – 夏期劇場 Sep 18 '11 at 10:23
  • If server 2 can accept multiple connections (which, if it's a default install of Apache, nginx.. almost any webserver, it will) then server 2 can process concurrent API requests. – Alex Sep 18 '11 at 10:27
  • Woah! so great again. :D You mean, the performance matter is not depend on the code. Just on the Web Server. :D Lol! Sooo thanks Alex. – 夏期劇場 Sep 18 '11 at 10:31
  • Well the code of server 2 will impact performance in that the function you're calling on server 2 takes a certain time to execute, and server 1 won't get a response for at least that long, of course. – Alex Sep 18 '11 at 10:39
  • Eh i've said i'm Ok at above. (That was testing locally). Now i'm testing with my network PCs. (API server on network pc). Now its showing "Access forbidden! Error 403". Why is it? – 夏期劇場 Sep 19 '11 at 10:27
  • This is becoming quite a long comment thread! That could be any number of things, you'll have to Google around. – Alex Sep 19 '11 at 10:35
  • So please kindly jump to my another thread http://stackoverflow.com/questions/7469839/curl-on-apache-showing-access-forbidden-error-403 :( – 夏期劇場 Sep 19 '11 at 10:57
  • @Alex, How do you implement the time changing hash on 'client' side and the verification on the remote script ? – tweetysat Jan 29 '15 at 12:17
  • @tweetysat I'm not a security expert, so I won't answer your question directly, as clearly getting the hashing part ‘wrong’ could result in arbitrary execution on the server two (although you'd be best off explicitly allowing only certain functions in `listening_page.php`). Might be worth opening a question. – Alex Jan 29 '15 at 13:54
0

You can do so by using an API. create a page on second server that takes variables and communicates to the server using those vars (depending on what you need). and the standard reply from that page should be either JSON or XML. then read that from server 1 by requesting that file and getting the reply from the 2nd server.

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

ahoura
  • 689
  • 1
  • 6
  • 16
  • So you are talking about a REST API? – Felix Kling Sep 18 '11 at 09:35
  • yup, but it depends on the user, REST API is good but if he prefers he can ofcourse use plain text as a reply. – ahoura Sep 18 '11 at 09:36
  • Buddies sorry for interrupting your talks. But, as i'm new with PHP, can PHP directly receive a JSON or XML result? I mean, JSON or AJAX results are returning to Javascript and print over HTML elements. (As far as i know very little about that.) – 夏期劇場 Sep 18 '11 at 09:51
  • @4lvin: JSON and XML are just a data formats. PHP has `json_encode` and `json_decode` to create and parse JSON. – Felix Kling Sep 18 '11 at 12:35
0

What you are aiming to do is definitely possible. You will need to set up some sort of api in order for server one to make a request to server 2.

I suggest you read up on SOAP and REST api

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

Generally you will use something like CURL to contact server 2 from server 1.

Google curl and you should quickly get idea.

Its not going to be easy to give you a complete solution so I hope this nudge in the right direction is helpful.

arkleyjoe
  • 131
  • 2
  • 3
  • Thanks for your suggestion about to use SOAP and REST APIs. But, Alex's way is simple & working. So can you please explain how is different between his solution and SOAP/REST APIs ? Thanks :) – 夏期劇場 Sep 18 '11 at 10:12
  • 1
    using a http get as suggested above is perfectly good solution but building an API will ultimately offer better functionality, security etc. If you are just querying for results then its quick and simple to use a get. If for example you wanted to authenticate users, post data to be inserted in to a DB etc then a publicly open get is probably not ideal. – arkleyjoe Sep 18 '11 at 11:28