Tobias Baumeister
Tobias Baumeister

Reputation: 2147

Execute PHP file on different server using PHP

I'm currently trying to add a second server for my website by dividing different services. One server takes care of the main part of my website, whereas the other one takes over my chat service.

However, sometimes the main server has to connect with the other server, preferably using only PHP files. This happens when a user changes their settings on the main server, for example, then the second server should be notified in the same moment (i.e. updating the database)

So for example when a user changes a setting on www.example.com/foo.php (Server 1), a request should be sent to www.sub.example.com/bar.php?setting=1&value=abc (Server 2), without showing the second request to the user. It should be made in the background, preferably even asynchronously to the user's request.

Does anyone know the best/easiest way to achieve this?

Upvotes: 0

Views: 1069

Answers (2)

rkovalenko
rkovalenko

Reputation: 131

E.g. post request with curl. Take parameters from post data and use them for creating request (you can use your own parameters, not from post data)

//set POST variables

$url = $_POST['url'];

unset($_POST['url']);

$fields_string = "";

//url-ify the data for the POST

foreach($_POST as $key=>$value) { 

    $fields_string .= $key.'='.$value.'&'; 
}

$fields_string = rtrim($fields_string,'&');

//open connection

$ch = curl_init();

//set the url, number of POST vars, POST data

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($_POST));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);


//execute post

$result = curl_exec($ch);

//close connection
curl_close($ch);

Upvotes: 1

herezy
herezy

Reputation: 336

Check out cURL: http://php.net/manual/en/book.curl.php

That's usually what I use for cross-domain notification.

Upvotes: 1

Related Questions