0

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?

2 Answers 2

1

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

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

Sign up to request clarification or add additional context in comments.

Comments

1

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);

2 Comments

Thanks for the example :) Though I don't quite understand why you're adding '&' signs in the foreach-loop, but then you're removing them again with rtrim. Can you just give an example on how a $fields_string should look like? I can easily generate them myself based on the necessary key/value pairs, I just want to know how they look like :)
He's only removing the last '&', but keeping the ones seperating each key/value pairs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.