How to send POST request to another website from a PHP script



Let’s say we have a website like somewebsite.com and there is an input form, but we don’t want to submit that form from that website, instead we send POST request to that page from another website. Here is a handy PHP script to do so:

<?php

$url = 'https://somewebsite.com/';
$data = array('key1' => 'value1', 'key2' => 'vaue2');

$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }

var_dump($result);

?>
loading...

Leave a Reply

Your email address will not be published. Required fields are marked *