cvs: pear /HTTP_Post Post.php package.xml

From: Date: Mon, 03 Dec 2001 14:33:34 +0000
Subject: cvs: pear /HTTP_Post Post.php package.xml
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-1457@lists.php.net to get a copy of this message
mj Mon Dec 3 09:33:34 2001 EDT Added files: /pear/HTTP_Post Post.php package.xml Log: * Initial checkin of HTTP_Post class (by Roko Roic <roko@tis.hr>)

Index: pear/HTTP_Post/Post.php +++ pear/HTTP_Post/Post.php <?php // +----------------------------------------------------------------------+ // | PHP version 4.0 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Roko Roic <roko@tis.hr> | // +----------------------------------------------------------------------+ // // $Id: Post.php,v 1.1 2001/12/03 14:33:34 mj Exp $ include_once 'PEAR.php'; /** * HTTP_Post - PHP class for posting a 'form' from within a php script * * version 0.5b * Copyright 2000 * Alan van den Bosch (alan@sanguis.com.au) * Sanguis Pty Ltd (acn 061 444 031) * * version 1.0 * Copyright 2001 * Tis.kis * PEAR comatibility and Error reporting changes by Roko Roic (roko@net.hr) * * I would also like to add that this functionality can be achieved by using * the CURL functions but one can not expect to find the curllib installed on * all servers and PHP compiled with curl enabled. * * Synopsis: * $http = new HTTP_Post(); * $http->setAction('http://127.0.0.1/index.php'); * $http->setElement('name','roko'); * $resp = $http->send(); * * @author Alan van den Bosch <alan@sanguis.com.au>, Roko Roic <roko@tis.hr> * @version 1.0 */ class HTTP_Post { /** * HTTP_Post() * Constructor used when creating a new instance of the HttpPost class. * Returns true on success. * ie. * $a = new HTTP_Post; */ function HTTP_Post(){ $this->_method = "post"; $this->_server = $GLOBALS["HTTP_HOST"]; $this->_file = "\\"; $this->_port = "80"; $this->_enctype = "application/x-www-form-urlencoded"; $this->_element = array(); $this->_timeout = 20; } /** * setServer * Set the server of the URI you wish to post to. see also setAction() * Returns true on success. * ie. * $a->setServer("127.0.0.1"); * or * $a->setServer("www.somehost.org"); * * @param String server */ function setServer($newServer = "") { if (strlen($newServer) < 1) { $newServer = $HTTP_HOST; } $this->_server = $newServer; return 1; } /** * setPort(string PORT) * Set the tcp port of the URI you wish to post to. see also setAction() * Returns true on success. * ie. * $a->setPort("8080"); * @param int newport */ function setPort($newPort = "80") { $newPort = intval($newPort); if ($newPort < 0 || $newPort > 65535) { $newPort=80; } $this->_port = $newPort; return 1; } /** * setFile(string FILENAME) * Set the filename of the URI you wish to post to. see also setAction() * Returns true on success. * ie. * $a->setFile("/incoming.php"); * @param string file */ function setFile($newFile = "\\") { $this->_file = $newFile; return 1; } /** * setAction(string ACTION) * Set the URI you wish to post to. * Returns true on success. * ie. * $a->setAction("http://www.somehost.org:8080/incoming.php3"); * @param string action */ function setAction($newAction = "") { $pat = "^((http://){1}([^:/]{0,}){1}(:([0-9]{1,})){0,1}){0,1}(.*)"; if (eregi($pat, $newAction, $sub)) { if (strlen($sub[3]) > 0 ) { $this->_server = $sub[3]; } if (strlen($sub[5]) > 0 ) { $this->_port = $sub[5]; } $this->_file=$sub[6]; return 1; } return 0; } /** * setEnctype(string ENCTYPE) * Set the encoding type used for the post. Can have the values * "application/x-www-form-urlencoded" or "multipart/form-data" * Returns true on success. * ie. * $a->setEnctype("multipart/form-data"); * @param string enctype */ function setEnctype($newEnctype = "application/x-www-form-urlencoded") { if ($newEnctype != "application/x-www-form-urlencoded" && $newEnctype != "multipart/form-data") { $newEnctype= "application/x-www-form-urlencoded"; } $this->_enctype = $newEnctype; return 1; } /** * setElement(array ELEMENTS) * Set or update a number of name/value pairs to be posted * Returns true on success. * ie. * $a->setElement(array("username" => "John Doe", * "password" => "dead-ringer", * "age" => "99")); * * setElement(string NAME, string VALUE) * Set or update a single name/value pair to be posted * Returns true on success. * ie. * $a->setElement("username","John Doe"); * @param mixed key * @param string val */ function setElement($key = "", $val = "") { if (is_array($key)) { $len = sizeof($key); reset($key); for ($i = 0; $i < $len; $i++) { $cur = each($key); $k = $cur["key"]; $v = $cur["value"]; $this->_element[$k] = $v; } } else { if (strlen($key) > 0) { $this->_element[$key] = $val; } } return 1; } /** * setTimeout(integer TIMEOUT) * Set the number of seconds to wait for the server to connect * when posting. minimum value of 1 second. * Returns true on success. * ie. * $a->setTimeout(10); * @param int timeout */ function setTimeout($newTimeout = 20) { $newTimeout = intval($newTimeout); if ($newTimeout < 1){ $newTimeout = 1; } $this->_timeout = $newTimeout; return 1; } /** * showPost() * Show the current internal state of an instance, for debugging. * Returns true on success. * ie. * $a->showPost(); */ function showPost() { $str = ""; $str.= 'Action:' . $this->_action . "<br />"; $str.= 'Server:' . $this->_server . "<br />"; $str.= 'Port:' . $this->_port . "<br />"; $str.= 'File:' . $this->_file . "<br />"; $str.= 'Enctype' . $this->_enctype. "<br />"; echo $str; $len = sizeof($this->_element); reset($this->_element); for ($i = 0; $i < $len; $i++) { $cur = each($this->_element); $key = $cur["key"]; $val = $cur["value"]; echo "Field:$key = $val<br>\n"; } return 1; } /** * send(boolean DISPLAY) * Send the name/value pairs using the post method. The response * can be echoed by setting DISPLAY to a true value. * Returns a string containing the raw response on success, PEAR_Error * on failure. * ie. * $a->send(1); * * @param Boolean display (echo response?) * @return PEAR_Error or HTTP response in a raw string */ function send($display = 0){ // open socket to server $errno = $errstr = $retstr = ""; $sk = @fsockopen( $this->_server, $this->_port, &$errno, &$errstr, $this->_timeout ); if ($sk) { $str = $this->_getRequestString(); fputs($sk, $str); while (!feof($sk)) { $resp = fgets($sk, 80); $retstr .= $resp; if ($display) { echo $resp; } } fclose($sk); return $retstr; } else { $errstr = 'Could not POST to HTTP server'; $dbgstr = $this->_server .':'. $this->_port . $this->_file; $dbgstr.= ' Fields?'; $len = sizeof($this->_element); reset($this->_element); for ($i = 0; $i<$len; $i++) { $cur = each($this->_element); $key = $cur["key"]; $val = $cur["value"]; $dbgstr .= "$key=$val"; if ($i+1 < $len) { $dbgstr .= '&'; } } return new PEAR_Error($errstr, 0, null, null, $dbgstr); } } /** * Returns a HTTP formatted request string * * @access private */ function _getRequestString() { $boundary = "----".md5(uniqid(rand())). "----"; $message = $this->_getMessage($boundary); $str = ""; $str .= strtoupper($this->_method) . " "; $str .= $this->_file . " HTTP/1.0 \r\n"; $str .= "Referer: \r\n"; $str .= "User-Agent: HTTP_POST/1.0 \r\n"; $str .= "Host: " . $this->_server . "\r\n"; $str .= "Content-type: " . $this->_enctype; if ($this->_enctype == "multipart/form-data") { $str .= "; boundary=" . $boundary; } $str .= " \r\n"; $str .= "Content-length: " . strlen($message) . "\r\n\r\n"; $str .= $message; return $str; } function _getMessage($boundary= ""){ $retstr = ""; $len = sizeof($this->_element); reset($this->_element); $switch = ($this->_enctype== "multipart/form-data") ? 0:1; for ($i = 0; $i < $len; $i++) { $cur =each($this->_element); $key =$cur[ "key"]; $val =$cur[ "value"]; if ($switch) { if (strlen($retstr) != 0) { $retstr .= "&"; } $retstr .= rawurlencode($key) . '='; $retstr .= rawurlencode($val); } else { $retstr .= $boundary. "\r\n"; $retstr .= "Content-Disposition: form-data; "; $retstr .= "name=\"$key\"\r\n\r\n$val\r\n\r\n"; } } if (!$switch) { $retstr .= $boundary . "\r\n"; } return $retstr; } } ?> Index: pear/HTTP_Post/package.xml +++ pear/HTTP_Post/package.xml <?xml version="1.0" encoding="ISO-8859-1"?> <!-- do not use the "Type" attribute here, that one is only for generated package.xml files --> <package> <name>HTTP_Post</name> <summary>PHP class for posting a 'form' from within a php script</summary> <maintainer> <user></user> <name>Roko Roic</name> <email>roko@tis.hr</email> </maintainer> <release> <version>1.0</version> <date>2001-12-03</date> <notes> </notes> </release> <filelist> <dir name="/" baseinstalldir="HTTP"> <file role="php">Post.php</file> </dir> </filelist> </package>
« previous php.pear.cvs (#1457) next »