Zend - The PHP Company




HTTP

Add Code


PostIt  

Type: code fragment
Added by: Darguz
Entered: 13/03/2000
Last modified: 02/12/2000
Rating: **** (6 votes)
Views: 11557
Let your PHP script pretend to be an HTML form! This function takes an associative array and a URL. The array is URL-encoded and then POSTed to the URL in HTTP 1.1 format.


<?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($URL0strpos($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.1n".
      
"Host: $Hostn".
      
"User-Agent: PostItn".
      
"Content-Type: application/x-www-form-urlencodedn".
      
"Content-Length: $ContentLengthnn".
      
"$ReqBodyn";

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


Usage Example


<?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;
  }
?>


Rate This Script





Search



This Category All Categories