Miscellaneous Code

  Home arrow Miscellaneous Code arrow Automatic form POSTer
MISCELLANEOUS CODE

Automatic form POSTer
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2002-07-27

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement
    This function will POST variables to a given URL. Very convienent when you want to POST without user intervention.

    By : Matt

    <?php
    /************************************************************************
    *
    * PostIt - Pretend to be a form.
    *
    * Copyright (c) 1999 Holotech Enterprises. All rights reserved.
    * You may freely modify and use this function for your own purposes. You
    * may freely distribute it, without modification and with this notice
    * and entire header intact.
    *
    * This function takes an associative array and a URL. The array is URL-
    * encoded and then POSTed to the URL. If the request succeeds, the
    * response, if any, is returned in a scalar array. Outputting this is the
    * caller's responsibility; bear in mind that it will include the HTTP
    * headers. If the request fails, an associative array is returned with the
    * elements 'errno' and 'errstr' corresponding to the error number and
    * error message. If you have any questions or comments, please direct
    * them to postit@holotech.net
    *
    * Alan Little
    * Holotech Enterprises
    * http://www.holotech.net/
    * December 1999
    *
    ************************************************************************/

    function PostIt($DataStream, $URL) {

    // Strip http:// from the URL if present
    $URL = ereg_replace("^http://", "", $URL);

    // Separate into Host and URI
    $Host = substr($URL, 0, strpos($URL, "/"));
    $URI = strstr($URL, "/");

    // Form up the request body
    $ReqBody = "";
    while (list($key, $val) = each($DataStream)) {
    if ($ReqBody) $ReqBody.= "&";
    $ReqBody.= $key."=".urlencode($val);
    }
    $ContentLength = strlen($ReqBody);

    // Generate the request header
    $ReqHeader =
    "POST $URI HTTP/1.1\n".
    "Host: $Host\n".
    "User-Agent: PostIt\n".
    "Content-Type: application/x-www-form-urlencoded\n".
    "Content-Length: $ContentLength\n\n".
    "$ReqBody\n";

    // Open the connection to the host
    $socket = fsockopen($Host, 80, &$errno, &$errstr);
    if (!$socket) {
    $Result["errno"] = $errno;
    $Result["errstr"] = $errstr;
    return $Result;
    }
    $idx = 0;
    fputs($socket, $ReqHeader);
    while (!feof($socket)) {
    $Result[$idx++] = fgets($socket, 128);
    }
    return $Result;
    }
    ?>

    <?php
    /*
    Sample code calling PostIt(). Note (no pun intended) that it is better to
    test for an error result with isset() rather than simply
    if ($Result["errno"])
    as the error number can be zero.
    */

    include("postit.php3");

    $d["foo"] = "some";
    $d["bar"] = "data";

    $Result = PostIt($d, "http://www.mydomain.com/test/test.php3");

    if (isset($Result["errno"])) {
    $errno = $Result["errno"]; $errstr = $Result["errstr"];
    echo "<B>Error $errno</B> $errstr";
    exit;
    }
    else {
    while (list($key, $val) = each($Result)) echo $val;
    }
    ?>
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Miscellaneous Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap