PHP: Remote Function Call and returning the result?

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).

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..? – 4lvin Sep 18 '11 at 9: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 9: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.. – 4lvin Sep 18 '11 at 10:08 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)? – 4lvin Sep 18 '11 at 10:23.

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.

– Felix Kling Sep 18 '11 at 9: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 9: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.) – 4lvin Sep 18 '11 at 9: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.

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 netmagazine.com/tutorials/make-your-own-... 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.

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 :) – 4lvin Sep 18 '11 at 10:12 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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions