Need code to execute a single php script that exists on multiple servers (without leaving the calling page)
I've got an updater.php script located on several of my sites. This updater.php file is set to execute code when called from a home base (my central server).
So I'm looking to create a dashboard of sorts in which all my remote site addresses are listed, with the path to this updater.php script like so...
www.server1.com/path/updater.php
www.server2.com/path/updater.php
www.server3.com/path/updater.php
...etc (there will be lots of them)
And I'll create an interface to list those along with checkboxes beside each one, and a select all, etc
And I'm looking to create a PHP script that will iterate over the whole collection of urls in that list and execute the call to the updater.php file on each server, passing it a "version=v001" for example...
$.get("http://server1.com/path/updater.php?version=v001");
$.get("http://server2.com/path/updater.php?version=v001");
...etc
I've already set up the code in updater.php (the file that resides in all my sites) so that when it receives a request, it parses the $_GET['version'] to see what the version is and it knows which file to go get on my central server to perform the update.
I'm just looking for some clues how to create the script for this dashboard that sets it all into motion...
开发者_运维技巧PS: In total, this is basically a batch updater script that executes wordpress theme updates without having to go to each site and do them individually.
It looks like you're using jQuery and that AJAX is fine to call the scripts rather than a PHP script. If that's not the case, ignore this.
When your "GO" button is clicked, use javascript to get the values of all the checked checkboxes. Loop through those, doing the $.get() thing. That should be all you need to do.
Optionally, you could catch the responses from the get() calls and update a status div to let you know whether they all ran successfully.
You might wanna check PHP's cURL extension which allows you to send request to multiple sites within your script quite easily.
If you need further assistance, let me know - I'll edit.
EDIT
You could use one of the javascript frameworks that would allow you to easily manage your AJAX calls (i.e. jQuery - imho the most stringent). Then do something similar to:
<form name="updater" id="updater" ...>
<input type="checkbox" name="server[]" value="0"/> Server 1
<input type="checkbox" name="server[]" value="1"/> Server 2
...
</form>
<script type="text/javascript">
var server_url = [
'www.server1.com/path/updater.php',
'www.server2.com/path/updater.php',
'www.server3.com/path/updater.php'
];
$('#updater').bind('submit', function() {
$('input[name="server[]"]:checked', this).each(function() {
$.get(server_url[this.value]);
});
});
</script>
Here you go!
I don't see the need for javascript. Honestly, I would just dump out a bunch of iframe tags which point to the appropriate urls if you're shaky on relying on php's abilities. Let the browser do the requests. Nice thing about the iframes is that you can have your remote scripts output "success" or error messages.
You can't use jQuery to do this alone because of the in-ability to do Cross Site Scripting.
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
// Run through each
foreach($_POST['servers'] as $serverHost)
{
// Now contact the server.
// if getting urls is disabled (which some hosts do)
// you can use CURL to connect to the server.
$result = file_get_contents($serverHost);
}
}
?>
<form method="post" >
<input type="checkbox" name="servers[]" value="http://foo1.com/updater.php" />
<input type="checkbox" name="servers[]" value="http://foo2.com/updater.php" />
<input type="checkbox" name="servers[]" value="http://foo3.com/updater.php" />
<input type="checkbox" name="servers[]" value="http://foo4.com/updater.php" />
<input type="checkbox" name="servers[]" value="http://foo5.com/updater.php" />
<input type="submit" />
</form>
精彩评论