HTML / PHP Post method for different servers

I want to create a POST method form that sends details to a PHP script to another server (i.e. not its localhost). Is it possible? I think GET is ok, is POST possible?

+5
source share
3 answers
<form method="POST" action="http://the.other.server.com/script.php">
+19
source

If you want to do this on your server (i.e. you want your server to act as a proxy server), you can use cURL to do this.

//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 want to send a POST request to another server, you can simply change the attribute action:

<form action="http://some-other-server.com" method="POST">
+12
source

, url-ify . , , , , ( ).

How to use arrays in POST CURL requests

If you really want to create this query string manually, you can. However, http_build_query will make the url-ify the data for POST section unnecessary. - Benjamin Powers 11/28/12 at 2:48 a.m.

0
source

All Articles