Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML/PHP Post method to different server

Tags:

html

post

php

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?

like image 204
rom Avatar asked Jan 30 '12 14:01

rom


2 Answers

<form method="POST" action="http://the.other.server.com/script.php">
like image 171
Marc B Avatar answered Oct 12 '22 16:10

Marc B


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">
like image 35
MMM Avatar answered Oct 12 '22 14:10

MMM