In demand


Friday, August 14, 2015

4 ways to Post information without a Form

Many times you want to send a POST request to a URL without using a form. This is becoming more common in web development because of the increased use and popularity of API's and webservices. For security and simplicity most systems allow only post headers in a request. While you could send a form with only hidden elements this is not a flexible solution and brings with it some unneeded complexities. An easier way is to just tell the browser to use the POST header to send data when opening a URL.  These examples are in PHP and javascript which are the most popular web scripting languages.

There are a few ways to do this and I recommend choosing one only after reading through all of the choices. The first using  the cURL library is the most commonly recommended method. Mostly because of it's flexibility in handling different situations like posting of cookies and authentication sequences.

$url = '';
$fields = array(
                        'lname' => urlencode($last_name),
                        'fname' => urlencode($first_name),
                        'title' => urlencode($title),
                        'company' => urlencode($institution),
                        'age' => urlencode($age),
                        'email' => urlencode($email),
                        'phone' => urlencode($phone)

//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

The second method is using  PHP's fopensoket() function.

function get_info()
    $post_data = array(
        'test' => 'foobar',
        'okay' => 'yes',
        'number' => 2

    // Send a request to
    $result = $this->post_request('', $post_data);

    if ($result['status'] == 'ok'){

        // Print headers
        echo $result['header'];

        echo '
'; // print the result of the whole request: echo $result['content']; } else { echo 'A error occured: ' . $result['error']; } } function post_request($url, $data, $referer='') { // Convert the data array into URL Parameters like a=b&foo=bar etc. $data = http_build_query($data); // parse the given URL $url = parse_url($url); if ($url['scheme'] != 'http') { die('Error: Only HTTP request are supported !'); } // extract host and path: $host = $url['host']; $path = $url['path']; // open a socket connection on port 80 - timeout: 30 sec $fp = fsockopen($host, 80, $errno, $errstr, 30); if ($fp){ // send the request headers: fputs($fp, "POST $path HTTP/1.1\r\n"); fputs($fp, "Host: $host\r\n"); if ($referer != '') fputs($fp, "Referer: $referer\r\n"); fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); fputs($fp, "Content-length: ". strlen($data) ."\r\n"); fputs($fp, "Connection: close\r\n\r\n"); fputs($fp, $data); $result = ''; while(!feof($fp)) { // receive the results of the request $result .= fgets($fp, 128); } } else { return array( 'status' => 'err', 'error' => "$errstr ($errno)" ); } // close the socket connection: fclose($fp); // split the result header from the content $result = explode("\r\n\r\n", $result, 2); $header = isset($result[0]) ? $result[0] : ''; $content = isset($result[1]) ? $result[1] : ''; // return as structured array: return array( 'status' => 'ok', 'header' => $header, 'content' => $content); }

The third and most simple method is to use PHP's HTTP constructs for build and receiving the request response.

$content = http_build_query (array (
'field1' => 'Value1',
'field2' => 'Value2',
'field3' => 'Value3'
$context = stream_context_create (array (
'http' => array (
'method' => 'POST',
'content' => $content,
$result = file_get_contents('', null, $context); 

The fourth way is to write javascript and XMLHttpRequest which more commonly called AJAX."POST","",true);

Today's Links

Post a Comment

Print this!