Jump to content

Sending post form from php


scs

Recommended Posts

Im modding my PHPBB registration.

Im going to have the form submit to a script I made. Then finish by sending back to the original phpbb page.

I dont know if u can, but I think its possible with php to submit a form from php. So instead of making a html form and making submit itself I would have php do it itself as one smooth motion. All behind the scenes. Like using curl or something like that.

 

Does anyone know how to do this?

Thanks

Zach

Link to comment
Share on other sites

Im a lil new to headers. Never really used them alot.

But I believe that may be the problem in the code. I've done similar code for sending an email with an attachment.

I'll see if I can get it to work.

If anyone who is good with heads knows what the problem is that would be great  ;D

Zach

Link to comment
Share on other sites

I found it  :D

It was here http://www.phpclasses.org/browse.html/package/375

You have to register to download the files. I did and tested it. It works. heres the files so you dont have to register

class.HTTPPost.inc:

<?php
/* 
    -
    HTTPPost ver 1.0.0
    Author: Daniel Kushner
    Email: daniel@websapp.com
    Release: 2 Nov 2001
    Copyright 2001
    www.websapp.com/classes
    -
        -

    Wilfried Wolf (wilfried.wolf@sandstein.de) modified two things on 04/18/02:
    1. dataArray can be any Array now, eg.
     $dataArray = array(
                   'firstInput' => '1',
                   'secondInput' => 
                     array(
                      'field1' => 'a',
                      'field2' => 'b'
                     )
                    );

    2. the post() method will now return the responseBody of the request,
    i.e. no hexadecimal numbers will show up when you try to output the response.
    the headers of the response as well as the body can now be accessed by new methods
    getResponseHeaders() and getResponseBody().
    if the post() method does not return anything, the error can be accessed by
    the new method getResponseError



    -
    Added basic authentication method by elias@hostrix.com on 12/12/01, 04:54:01PM.
    new usage: HTTPPost($url, $dataArray, array('username', 'password'))
    -

    -
    Posts an array of data to a given URL using rfc2616 - Hypertext Transfer Protocol -- HTTP/1.1
    The data array should be in the form of comma-separated key => value pairs.
    Both the data and URL can be passed in the constructor or added by using
    the functions setDataArray() and setURL.
    -
*/
class HTTPPost {

    var $url;
    var $uri;

    var $dataArray = array();

    var $responseBody = '';
    var $responseHeaders = '';

    var $errors = '';
    
    function HTTPPost($url = '', $dataArray = '', $authInfo = false) {
        $this->setURL($url);   
        $this->setDataArray($dataArray);
        $this->authInfo = $authInfo;
    }
    
    function setUrl($url) {
        if($url != '') {
            $url = ereg_replace("^http://", "", $url);
            $this->url = substr($url, 0, strpos($url, "/")); 
            $this->uri = strstr($url, "/"); 
            return true;
        } else {
            return false;
        }
    }
    
    function setDataArray($dataArray) {
        if(is_array($dataArray)) {
            $this->dataArray = $dataArray;
            return true;
        } else {
            return false;
        }
    }

    // can be called as: setAuthInfo(array('user', 'pass')) or setAuthInfo('user', 'pass')
    function setAuthInfo($user, $pass = false)
    {
      if (is_array($user))
        $this->authInfo = $user;
      else
        $this->authInfo = array($user, $pass);
    }

    function getResponseHeaders(){
        return $this->responseHeaders;
    }

    function getResponseBody(){
        return $this->responseBody;
    }

    function getErrors(){
        return $this->errors;
    }

    function prepareRequestBody(&$array,$index=''){
        foreach($array as $key => $val) {
    if(is_array($val)){
	if($index){
                    $body[] = $this->prepareRequestBody($val,$index.'['.$key.']');
                }
                else {
                    $body[] = $this->prepareRequestBody($val,$key);
                }
            }
            else {
                if($index){
                    $body[] = $index.'['.$key.']='.urlencode($val);
                }
                else {
                    $body[] = $key.'='.urlencode($val);
                }
            }
        }
        return implode('&',$body);
    }

    function post() {

        $this->responseHeaders = '';
        $this->responseBody = '';

        $requestBody = $this->prepareRequestBody($this->dataArray);
        
        if ($this->authInfo)
          $auth = base64_encode("{$this->authInfo[0]}:{$this->authInfo[1]}");

        $contentLength = strlen($requestBody); 
        
        $request = "POST $this->uri HTTP/1.1\r\n". 
                   "Host: $this->url\r\n". 
                   "User-Agent: HTTPPost\r\n". 
                   "Content-Type: application/x-www-form-urlencoded\r\n". 
                   ($this->authInfo ? "Authorization: Basic $auth\r\n" : '') .
                   "Content-Length: $contentLength\r\n\r\n". 
                   "$requestBody\r\n"; 

        $socket = fsockopen($this->url, 80, &$errno, &$errstr); 
        if(!$socket) { 
            $this->error['errno'] = $errno;
            $this->error['errstr'] = $errstr; 
            return $this->getResponseBody();
        }
        
        fputs($socket, $request); 
        
        $isHeader = true;
        $blockSize = 0;

        while (!feof($socket)) { 

            if($isHeader){
                $line = fgets($socket, 1024); 
                $this->responseHeaders .= $line;
                if('' == trim($line)){
                    $isHeader = false;
                }
            }
            else {
                if(!$blockSize){
                    $line = fgets($socket, 1024);
                    if($blockSizeHex = trim($line)){
                        $blockSize = hexdec($blockSizeHex);
                    }
                }
                else {
                    $this->responseBody .= fread($socket,$blockSize);
                    $blockSize = 0;
                }
            }
        }
        fclose($socket);
        return $this->getResponseBody(); 
        
    }
}
?>

example.php:

<?php
  
include('./class.HTTPPost.inc');

$arr['name'] = 'Daniel';
$arr['address'] = 'New York';
$arr['email'] = 'daniel@websapp.com';
$arr['foo']['bar'] = 'whatsoever';
$arr['foo']['bar']['query'] = 'something else';

$post = new HTTPPost('http://localhost/postCollector.php', $arr);
$result = $post->post();

if($headers){
print nl2br($post->getResponseHeaders());
}

print $result;

/* equivalent to:
print $post->getResponseBody();
*/


?>

postCollector.php:

<?php
if(is_array($HTTP_POST_VARS) && count($HTTP_POST_VARS) > 0) {
    echo "And here are the results of your post:<br>";
    print_r($HTTP_POST_VARS);
}
else {
    echo 'Oops, nothing in $HTTP_POST_VARS?<br>';
}
?>

Link to comment
Share on other sites

I've not tested that code but from a precursory scan it looks like it just communicates with the page and not redirects to (which is what i'm after but maybe not you), if it is what happens, then this is what I was doing yesterday... http://www.phpfreaks.com/forums/index.php/topic,176887.0.html, a lot simpler, and not hard to add authentication and cookie handling just by following the https specification (http://www.faqs.org/rfcs/rfc2616).

Link to comment
Share on other sites

In this example it uses opensocket. If you change that to header, It might work. The example here is different than what you were trying to use.

 "POST $this->uri HTTP/1.1\r\n". 
                   "Host: $this->url\r\n". 
                   "User-Agent: HTTPPost\r\n". //<<< main difference I pick out
                   "Content-Type: application/x-www-form-urlencoded\r\n". 
                   ($this->authInfo ? "Authorization: Basic $auth\r\n" : '') .
                   "Content-Length: $contentLength\r\n\r\n". 
                   "$requestBody\r\n"; 

You could try that, let me know if that helps at all

Link to comment
Share on other sites

No, it's the same as my other post, but with extra info such as 'User-Agent' and 'Authorization', as said, there just additional. If I realised this was how you wanted to do it I would have redirected you to that other post. I however need to actually redirect whilst adding POST vars, not just send them and get result.

Cheers...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.