PHPisfree
PHPisfree

Reputation: 1

Sending data to third-party server then retrieving data back

Is this possible within PHP:

  1. User fills in form on my site
  2. The form submits the data from the form to a third-party server somewhere else on the net, essentially handing off the data to the third-party server somehow
  3. Said third-party server does something with the data and then generates a numerical value to send back to my PHP script
  4. My server/PHP script gets that numerical value/data for use in the script again

Is it doable in PHP? Does PHP have inbuilt functions to perform the above tasks? Would such a thing require tons of advanced code or is it relatively easy to do?

Thanks in advance for any help on the matter

Upvotes: 0

Views: 1148

Answers (2)

zomboble
zomboble

Reputation: 833

Yes, when you send your form, send it to the server you want with method of POST. Which would look like:

<form action="www.siteURL/pageToParseCode.php" method="post">
  First name: <input type="text" name="fname" /><br />
  Last name: <input type="text" name="lname" /><br />
  <input type="submit" value="Submit" />
</form>

At the server its sent to you will want to do something like:

$field1 = $_POST['field1name'];

On the server which will manipulate the data, you can then post it back to your server using someting like curl, if you dont totally understand curl take a look at that link there, or you could use a php header, and set the data you want to send back into the url using a get method, so the url the user receives would look like this using the get method:

www.yoursite.com/index.php?variable1=value1&variable2=value2 and so forth, then interpret that like so:

if (isset($_GET['variable1'])) {
$var = $_GET['variable1'];
}

Upvotes: 0

Jacob Lauritzen
Jacob Lauritzen

Reputation: 2840

You can use cURL for that

$urltopost = "http://somewebsite.com/script.php";
$datatopost = $_POST; //This will be posted to the website, copy what has been posted to your website

$ch = curl_init ($urltopost);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$returndata = curl_exec ($ch); //This is the output the server sends back

Upvotes: 1

Related Questions