I want to create a POST method form that sends details to a PHP script on another server (ie, not its localhost). Is this even possible? I imagine GET is fine, so is POST possible?
Asked
Active
Viewed 2.9k times
3 Answers
22
<form method="POST" action="http://the.other.server.com/script.php">

Marc B
- 356,200
- 43
- 426
- 500
-
1thats great, but the question is: does the POST data actually get received at the external php script? Isn't post data handled locally by apache? – rom Jan 30 '12 at 14:59
-
The data goes to whatever URL you specify in the `action`. If that's on another server entirely, it goes there and would not ever touch the server that the form was loaded from. – Marc B Jan 30 '12 at 15:00
-
@rom — POST data is handled 'locally' by the server that receives the HTTP request. – Quentin Jan 30 '12 at 15:03
-
It is recieved. The only probability that can cause it SEEMS it doesn't recieve: a) the name you use for values is not the same b) it checks referrer. – axiomer Jan 30 '12 at 15:05
-
Something to bear in mind is that obviously you are consequently redirecting the user to that other server so he does not stay on your website. – MMM Jan 30 '12 at 15:07
-
1@Ben D: outright false. POST data is sent in the message body. GET data goes "in the headers", because it's part of the requested URL. – Marc B Jan 30 '12 at 15:09
-
@Marc: thanks for the clarification. That explains why POST data doesn't have the strict default length limits that GET has. Appreciate it. – Ben D Jan 30 '12 at 15:18
11
If you want to do that on your server (i.e. you want your server to act as a proxy) you can use cURL for that.
//extract data from the post
extract($_POST);
//set POST variables
$url = 'http://domain.com/get-post.php';
$fields_string = "";
$fields = array(
'lname'=>urlencode($last_name), // Assuming there was something like $_POST[last_name]
'fname'=>urlencode($first_name)
);
//url-ify the data for the POST
foreach($fields 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($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
However if you just simply want to send a POST request to another server, you can just change the action
attribute:
<form action="http://some-other-server.com" method="POST">

MMM
- 7,221
- 2
- 24
- 42
-
1I just didn't know it was possible to post across servers. I don't know how the POST method works the way it does. Thanks. – rom Jan 30 '12 at 15:31
0
There is another question on Stack Overflow that shows a better way to url-ify the variables. It is better because the method shown in the answer above breaks when you use nested associative arrays (aka hashes).
How do I use arrays in cURL POST requests
If you are really wanting to build that query string manually, you can. However, http_build_query will make your "url-ify the data for the POST" section unnecessary. – Benjamin Powers Nov 28 '12 at 2:48

Community
- 1
- 1

mishawagon
- 28
- 4