How to trigger a php file on another server

Hello gifted fingers!
I need to complete a project and one of my challenge is calling a php file in an external server to run any time a form in another server or website is submitted.

Lets say, websiteA holds the form and once submitted it should run the file in websiteB
The file in websiteB function is to manually check database, send email and sms
But i don’t want to put the url in the form action="" so that the page will not be redirected to.
I don’t want to use ajax call, am looking at curly or using API but dont know the right way to turn to, any help is highly appreciated.

At this point, you have eliminated all possibility of sending the data.

I don’t know what you mean by curly.
Using an API would be an AJAX call.

CURL, possibly?

The API call could refer to an API that the other server exposes, callable directly from PHP, not necessaril Ajax. Submit the form to a script on server A, which then calls server B using CURL or the API.

JavaScript perhaps?

Tell us about the real problem you are trying to solve by doing this. What is the high level overview of what you have going on?

So you just need to open a url from the serverside. Use file_get_contents

Sorry for typing error i mean to say curl
Like curl_init();
Why i seems not to like using ajax be cause I know is a JavaScript function and what if the user disables JavaScript in his browser? I want a pure server side approach to it because is a security feature

Yes @droopsnoot you got exactly what i wanted, please can anyone help with codes on how to archive this? No JavaScript or Ajax call. Any server side language is most preferred

I only have two task to accomplish.
I don’t need to use file_get_contents() because i dont intend to pass the code to website A

My objective is this:

  1. The database in server B should not be know to serverA where the form is, if not i would have just added the database details inside the form in website A, since is all about quering a database and getting a result.

But I want a way of hiding database querying php file in serverB so server A don’t know which database was queried all it needs to know is if result returned from server B is Yes then do, but if returned result is No then do that. So anything server side interaction or call is most preferred, not JavaScript or ajax.

  1. The second task is to have a way to make a php run in an external server following response from another server or website.
    Example, an email sending php file is on serverB but it only execute or send emails when ever serverA makes a call to it.
    And probably Server A should be able to send which email address that serverA should send the email to.

A simple illustration is ServerA telling a php file in ServerB to send email to this email address. The email address to send the email to will come from a form submitted on ServerA

These two statements do not square, which is why the statement I gave was that he had eliminated all possibility. You have to submit the data SOMEWHERE, SOMEHOW.

This is what droop is describing.

Server A can ‘call’ a page in Server B by doing things as simply as file_get_contents('http://serverb/doathing.php?data1=s') (as long as fopen_url is enabled, which… pretty much every PHP environment allows this.)

What I meant was that the OPs form action would post to a page on his server A, and that page would then call the script on server B. But it’s just a level of abstraction, most of the suggestions on here would allow it to work.

Well, you need to write something using CURL to access the other server, or use file_get_contents() as has been posted here too. There are plenty of examples of how to do that.

Do you have control over both servers? That is, are you writing the code on server B, so you can define exactly what you need to pass to it, or how to call it?

am thinking using file_get_content (www.serverb.com/thefile.php) will output all the content in that file as html, and is not used to get dynamic data especially data result from database query.
i can just do

if(isset($_POST[postfroma])){
curl_init();
curl_url(www.serverb.com/thefile.php)

// full curl function to post and get result
}

yes i have control over both servers

This sounds like a nice use case for a webhook. Have you looked into this as an option?

@mark334 nop i have not considered that but am open to try any approach that works and is secured, you can please give me a lead

If you have control over both servers, then you could have the script on the remote server return exactly what you want, in the format you need it. It will only output a load of html if that’s what you tell it to do.

Hi @pandglobal,

Sure thing. I contributed this piece last month: https://automatorplugin.com/whats-a-webhook/

You can start there. There are links for further reading as well.

I hope that helps! :slight_smile:

Thanks @mark334 am looking and studying the link you sent, but so far this is where i have gotten to and i will post the full source code in a comment below perharps you can look to it too

Here is the summary of what i want to archive and how far i have gone using curl which seems to do my task perfectly well by triggering a file in another sever to run and update the database, but my problem now is that the database updated is not with the data or values sent from the serverA where the html form is.

<!--- html code -->
<form action="" method="post" name="cashaform" id="cashaform">
<div class="dephone">Phone <br><input type="text" name="phone" id="phone" /></div>
<div class="deemail">Email <br><input type="text" name="email" id="email" /></div>
<div class="submit"> <br><input type="submit" name="submitprime" value="Submit" id="submitprime" /></div>

</form>

// the code that processes the form and sends it to do.php on another server that updates the database with details from the form

// This is process.php in serverA
if(isset($_POST['submitprime'])){
	
	$sphone = $_POST['phone'];
	$semail = $_POST['email'];

	$form_data = array(
	'phone' => $sphone,
	'email' => $semail
	);

	$str = http_build_query($form_data);

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'http://localhost/serverb/do.php');
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$output = curl_exec($ch);
	curl_close($ch);
}

Now on serverB i have this file called do.php which updates the database once the process.php hits it through curl

// do.php that is on serverb
$email = 'email@example.com';
$phone = '01234567';


// update the database
$run = "INSERT INTO prime_securetable (email, phone) VALUES('$email', '$phone')";

if(mysqli_query($conn, $run) ) {
	echo 'Transferred Successfully';
}else{
	echo'transfer failed';
}

mysqli_close($conn);

I want the data phone and email that will be used to update the database be the one sent from the post from form in serverb, i tried it and set fixed values using email@example.com and a fixed phone 01234567 but i want this values to be what was actually submitted from the form in serverA
and it updates the database, but i want it to be values from the form posted through curl.
Any help please.