Arya
Arya

Reputation: 8995

PHP to execute PHP on another server and get the result

My shared hosting does not support postgresql php functions, but I need to load some images from postgresql which is hosted remotely on a dedicated server.

Would it be possible to have a PHP script on the server with postgresql and then another PHP script on the shared hosting would call the script on the postgresql server to get the images from the dedicated server to the shared hosting but I don't want the IP or domain of the dedicated server ever to be revealed.

Would this be possible? if so how?

Upvotes: 3

Views: 1354

Answers (4)

low_rents
low_rents

Reputation: 4481

I just came up with another idea that is much better imho; it also might be better in terms of security.

use a PHP script that simulates being an image file:

// example code for getting binary image data from db 
// (PDO and MySQL in this example)

$stmt = $pdo->prepare( "SELECT image_bin FROM image_table WHERE id = :id LIMIT 1" );
$stmt->bindValue(":id", $_GET["id"], PDO::PARAM_INT);
$stmt->execute();
$image_bin = $stmt->fetchColumn();

header('Content-Type: image/png');
echo $image_bin;


on the page where you want to show the image you don't even need to use PHP:

<img src="http://myimageserver.com/image.php?id=3425">


in this example you just need to use the GET variable id to define/decide which image is shown. most of the work is done on the remote server where you could grab the desired image by using POSTGRESQL like you mentioned.

Upvotes: 0

user5672329
user5672329

Reputation:

Yes it's possible. However, you need to make a JSON API: the server wich serve postgresql needs to fetch results from Database, then encode it in json with json_encode() method and return it with echo.

Then, you need to make an HTTP request from your shared hosting using CURL, here's an example:

function GetContent($url) {
    $curl = curl_init();
    $ua   = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl ,CURLOPT_USERAGENT, $ua);
    curl_setopt($curl, CURLOPT_COOKIESESSION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    $return = curl_exec($curl);
    curl_close($curl);
    return $return;
}

Then, call the function:

$sql = json_decode(GetContent('http://urlofyourserver/testJSON.php'));

You can now get the result in the $sql variable :)

Oh and don't forget, make a password request using $_GET requests on your server wich serves postgresql, otherwise everyone will be able to access to data if they found your server URL.

e.g: http://urlofyourserver/testJSON.php?secret=xxx

You can also pass the query as a get parameter, but It can be risked if your password is cracked.

Postgresql server sample

$realPass = "blablah";
if(isset($_GET['secret'])) {
   $pass = $_GET['secret'];
   if($pass === $realPass) {
     header('Content-type: application/json');
     // do query here and return it as $test var
     echo json_encode($test);

   }
}

Upvotes: 2

low_rents
low_rents

Reputation: 4481

it's even much easier with file_get_contents():

$img_binary = file_get_contents("https://i.imgur.com/1H9Ht5a.png");
$img_base64 = base64_encode($img_binary);
echo '<img src="data:image/png;base64,' . $img_base64 . '">';

plus: it's a standard PHP method where "cURL" might not be activated in every PHP environment.

be careful with the type of your image. replace data:image/png; with data:image/jpeg; or other image formats accordingly.

Upvotes: 2

L Bahr
L Bahr

Reputation: 2901

You could use php curl

Example that will send the get parameters get1 and get2 to a test.php on a server with IP 192.168.2.2

// Get cURL resource
$curl = curl_init();

// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://192.168.2.2/test.php?get1=value&get2=value2',
    CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'
));

// Send the request & save response
// The response could be JSON with the results from the database
$resp = curl_exec($curl);

// Close request to clear up some resources
curl_close($curl);

You could use PHP to encode and decode your results in JSON to make things easier. To do this use json_encode() and json_decode()

If you want some more information check out:

Upvotes: 0

Related Questions