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).
Return value from include on a remote file
Try using file_get_contents() instead of include.
This writes the file into a variable [causing the script to execute remotely], but won't run the response locally.
Alternatively If you have the ability to use cURL, it is safer and probably quicker.
A small snippet to remotely file_get_contents();
function curl_get_contents($url){
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($c);
if (curl_getinfo($c, CURLINFO_HTTP_CODE) > 400) $res = false;
curl_close($c);
return $res;
}
By way of explanation
- RETURNTRANSFER puts the response of the curl request into a variable (instead of printing to screen).
- CURLOPT_FOLLOWLOCATION has not been set, so if the page has been moved, curl will not follow it. You can set that, or have it set based on a second argument.
- HTTP_CODE, if above 400 (an err code, presumably 404 or 500) will return false instead of the fancy custom 404 page that might be setup.
In my testing, get_headers() is more reliable than curlinfo_http_code but requires a second call to the page being included, which can make things go awry.
eg.if (!strpos(200, get_headers($url)[0])) return false;
How to have a listener PhP script respond to GET requests
You don't need to implement a listener, the web server is doing that for you. Just create a script and put it in your web directory. When the web server receives a GET request to that script, it will execute the PHP script.
In the script, you can just echo the values that you want to send back in a format that your calling function understands. (Of course you could also set some headers, if you feel that this is necessary).
Calling php code from php code on different server
$output = file_get_contents('http://your.remote.server/page.php?args=12345');
Or you can use cURL. Keep in mind that when you're executing files in PHP you should limit the ability for other people to do so(ie. password protect the page/directory) and escape the shell arguments before running them.
Related Topics
Strip HTML Tags and Its Contents
How to Pass Variables Between PHP Scripts
Jquery UI Saving Sortable List
Cakephp Cake_Core_ Cache Was Unable to Write 'Cake_Dev_En-Us'
Laravel 5.1 Date_Format Validation Allow Two Formats
Escaping SQL Queries in Codeigniter
Php: What Is the Complexity [I.E O(1),O(N)] of the Function 'Count'
How to Upload Image PHP and Insert Path in MySQL
How to Check If File Is Ascii or Binary in PHP
Incrementing Numbers Starting from 0000 in PHP
How to Determine the Extension(S) Associated with a Mime Type in PHP
Redirect to 'Www' Before Ssl Requirement
PHP - Fastest Way to Check Presence of Text in Many Domains (Above 1000)
PHP Twitter API - How to Pull in Multiple Users Tweets
Convert This String to Timestamp PHP
How to Get Column Names from Pdo's Fetchall Result
PHP Mail Function: Legitimate Mails Marked as Spam by Gmail and Hotmail