use http to run query on remote sql db
I'm trying to connect to a sql db through php but it seems that my hosting provider has blocked access to connect to remote db's so I've been told to use http and "parse a key" to my other hosting account (with a different hosting provider) and run the mysql queries from there and then send the data back to the original.
I need to connect to the db and check for certain info in certain rows. Then by using the if
function, processing different code depending on what come back from the db (if the info is correct and is in the correct row, process x code and if the info is not correct in the开发者_StackOverflow中文版 db, process y code.)
^^thats the query i'm trying to run^^
I have it all working other then the actual connecting to the db part as my hosting provider has blocked it. They told me to use http (as explained above) but i have no idea how to do so.
Can anyone explain (in English please) how i would use http to do something like this? Or better, an easier solution?
I cant change hosting providers....
Thanks
You can use CURL to request that php page on another server and then process the response.
Example of creating curl request
$encoded = ''; // include GET variables foe example. POST will be similar
foreach($_GET as $name => $value) {
$encoded .= urlencode($name).'='.urlencode($value).'&';
}
// chop off last ampersand
$encoded = substr($encoded, 0, strlen($encoded)-1)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "myOtherHostThatBlockedMysqlAccess.com/mysqlPage.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
$output = curl_exec($ch);
curl_close($ch);
So - what we do in the previous example - we request mysqlPage.php on the server that has access to your Mysql db. In that page - you'll have to put your Mysql access logic. After you retrieve records from db - youll probably end up with some array like $someDbArray- you can serialize this array and simply echo it out:
echo serialize($someDbArray);
Whatever you echo out will be available to you inside of the $output variable that is the CURL output. So you can simply retireve your array like this:
$myDataArray = unserialize(output);
However - if you ask me - the best solution will be to get a hosting that doesn't limit access from anywhere. I assume you're using shared hosting? I'd switch to at least VPN or dedicated server. I mean - either way you go - via REST or CURL - latency will be tremendous. Also - I'm not sure what kind of data you'll be transfering but think of a security. You'll have to use https at the very least.
Better you build some REST service in your remote site. That way, it can provide a secure and convinience data-mining (read/write) with standard HTTP mechanism.
If you can run a daemon on your server then give DBSlayer a try. It provides an HTTP interface for MySQL, among other things.
精彩评论