Forum Moderators: coopster
I need your wisdom again....it always works :)
anyway, I have 2 web servers and only one has a dbase which does not allow remote access. what I need is to get some info from the database for my second web site...is there any way to do this?
-My idea is to create a php page in the second server which will send 'manually' an id ($hotel_id = '1'; and auto submit) to the page of the first server...the latter does all queries and calculaitons and sends back to the 2nd server page all data needed.....is there a better way? If I am not clear plz tell me!
Hope there is a way!
Thx in advance
What you've described is effectively a "Web Service". You need to write a PHP script on the server with the database that takes some input parameters, performs the query, and returns the results in some pre-defined format.
You can then call your Web Service "behind the scenes" on the second server. You don't even need to do anything fancy like SOAP or XML; you can make it very simple indeed, using nothing more than PHP's fopen() function. As an extremely basic example; here's how you could write a web service to add 2 numbers together and return the result:
On the first server:
addnumbers.php
<?php
header("Content-Type: text/plain");
$a = $_GET["a"];
$b = $_GET["b"];
print ($a+$b);
exit();
?>
And then on the second server:
<?php
print "5 + 7 is ";
$file = fopen("http://www.example.com/addnumbers.php?a=5&b=7","r");
$result = fread($file,1024);print $result;
?>
...and that's it! All you need to do is implement something along the lines of the above; but of course your web service script on the server would be a bit more complicated doing the database work etc; but the principle is the same.
One option you might want to consider for returning your results from Server A to Server B is through PHP's serialize() [uk2.php.net] and unserialize() [uk2.php.net] functions. Here's how you could transfer an array from Server A to Server B using almost the same code as above:
On the first server:
getrhyme.php
<?php
header("Content-Type: text/plain");
$rhyme[1] = "Mary had a little lamb";
$rhyme[2] = "She put it up on eBay";
print serialize($rhyme);
exit();
?>
And then on the second server:
<?php
print "Here is a rhyme:<br>"
$file = fopen("http://www.example.com/getrhyme.php","r");
$rhyme = fread($file,1024);
$rhyme = unserialize($rhyme);
foreach($rhyme as $line)
{
print $line."<br>";
}
?>
If you do something along these lines; you could simply serialize() the output of mysql_fetch_array() [uk2.php.net].
Hope this helps!
sqlproxy.php:
<?php
header("Content-Type: text/plain");mysql_connect("localhost","username","password");
mysql_select_db("database");
$result = mysql_query($_GET["sql"]);
print serialize(mysql_fetch_array($result));
?>
..just some food for thought!