I would like make a script using PHP (probably need JS) to send POST data to another webpage and get the result back.
For example, Domain A will have a form with a textbox and submit button, and Domain B will have a script which will fill the textbox and press the submit button and return the generated HTML page.
The visitor's web browser uses HTML code to display the form. When the form is submitted, the browser sends the information to the backend using the link mentioned in the "action" attribute of the form tag, sending the form data to that URL.
In HTML, linking submit buttons using the Anchor Tag is a simple and dependable approach. Write/Declare a Submit button between the Anchor tag's Starting and Closing tags. Give a Path where you wish to link your Submit Button by using the href property of the Anchor element.
In HTTP, there are two ways to submit HTML forms to the server using the application/x-www-form-urlencoded and multipart/form-data content types. HTML forms can be submitted from browsers and from code. Submitting HTML forms using the application/x-www-form-urlencoded media type.
Go to Formsand open your form. Click Send. Click the HTML and click Copy. Paste the HTML into your site or blog.
The following lines of code can be written on another php script,
//set POST variables
$url = 'the website from which you need data through post';
$fields = array(
//post parameters to be sent to the other website
'text'=>urlencode($_POST['text']), //the post request you send to this script from your domain.
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
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);
Now $result will contain the text received from the other website.
With JS: for security reasons not. Read on Same origin policy.
With PHP you can do what you want, including POST
ing other servers. For example use CURL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With