How to simulate a html form "submit" using PHP ?

Author Message

H-Works Agency

Thursday 19 June 2008 5:57:31 am

Hello !

I want to simulate a html form submit using php. How can i do that ?

I tried curl or fsocketopen but it return the result to my php page instead of redirecting to the POST destination url.

For example my form in html would be :

<form action="http://domain/destination.php">

In php i would like something like that :

phpfunctionSubmit($myPostVars, 'http://domain/destination.php');

This command would submit $myPostVars array and redirect to http://domain/destination.php.

EZP is Great

Pascal Specht

Thursday 19 June 2008 6:10:00 am

Hi Martin,

maybe nonsense, but would @readfile('http://the-url’) (of course with encripted GET arguments instead of POST) be a work-around?

</Pascal>

H-Works Agency

Thursday 19 June 2008 6:44:50 am

Thanx but i am stuck with POST.

This topic concern my eZSips (and new eZOgone) payment extension.

I am trying to get rid of the useless payment page which is only asking the payment type.

I want to put this choice on the basket, then the module would auto submit the correct values.

EZP is Great

Pascal Specht

Thursday 19 June 2008 7:12:43 am

Cool idea as to get rid of the intermediate page!

What about then looking into how gateway selection works, find out where it is called (eZPaymentGatewayType ? ), and modify the calling code there in the first place instead of doing a hack with curl?

Unfortunately I'm pretty busy in another type of development (prices from matrix instead of product class) and can't spend much time in investigating further.

</Pascal>

Steven E. Bailey

Thursday 19 June 2008 7:26:30 am

You've got to do something like this:

 $query="Login=".$username."&Password=".$password."&LoginButton=Log+in&RedirectURI=";
        $fp = fsockopen($site, $socket, $errno, $errstr, strlen($site));
        if (!$fp) exit();
        fputs($fp, "POST /".$adminsite."/user/login HTTP/1.1\r\n");
        fputs($fp, "Host: ".$site.":".$socket."\r\n");
        fputs($fp, "User-Agent: $user_agent\r\n");
        fputs($fp, "Referer: http://".$site.":".$socket."/".$adminsite."\r\n");
        fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
        fputs($fp, "Content-length: " . strlen($query) . "\r\n\r\n");
        fputs($fp, $query."\r\n");
        //fputs($fp, "Keep-Alive: 300\r\n");
        //fputs($fp, "Connection: keep-alive\r\n");

        fputs($fp, "Connection: close\r\n\r\n");
        fclose($fp);

Certified eZPublish developer
http://ez.no/certification/verify/396111

Available for ezpublish troubleshooting, hosting and custom extension development: http://www.leidentech.com

H-Works Agency

Thursday 19 June 2008 7:48:23 am

Uhm that look very interesting thanx a lot.

I will try this code now.

EZP is Great

Łukasz Serwatka

Thursday 19 June 2008 8:11:29 am

Hi, tool is out there already
eZHTTPTool::sendHTTPRequest()

Personal website -> http://serwatka.net
Blog (about eZ Publish) -> http://serwatka.net/blog

H-Works Agency

Thursday 19 June 2008 8:54:45 am

Great Lukasz...that is exactly what i was expecting...gonna test that.

EZP is Great

H-Works Agency

Friday 20 June 2008 11:53:03 am

Lukasz i tried your function but its not doing what i want :

eZHTTPTool::sendHTTPRequest($uri,$port,$post);

This function is posting the data (well i guess) but i am not redirected to my $uri address like a simple html form submit would.

Well at least i don't see any change in my address bar, i am still on the php page where the function is. Am i technically on the remote site ?

Is that possible with this function ?

EZP is Great

Felix Laate

Saturday 21 June 2008 1:59:48 am

Hi Martin,

maybe you could do a redirect after posting.

From the API:

static eZHTTPTool::redirect  ( $path,
		$parameters = array(),
		$status = false	 
	) 	

Felix

Publlic Relations Manager
Greater Stavanger
www.greaterstavanger.com

H-Works Agency

Monday 23 June 2008 11:17:49 am

Hello Felix,

I am still stuck on this thing...which seems simple at the start... :\

What i try to do :

$url = 'https://secure.ogone.com/ncol/test/orderstandard.asp';
$port = 80;
$post = array('myvar' => 'test');

eZHTTPTool::sendHTTPRequest($uri,$port,$post,'momo', true);

But if i do a redirect after that then data won't be posted ?

I need to redirect to $url as if i use a simple html form with :

<form action="{$url}" method="POST"> 

At the end i could use a javascript "onLoad=submit()" but its not a clean way as my template would be loaded from extension's php code.

EZP is Great

Mattia Ago

Saturday 05 July 2008 8:12:51 am

sorry my ignorance... where I can find the API for the function:

static eZHTTPTool::redirect ( $path, 
                $parameters = array(), 
                $status = false  
        )       

thanks a lot :)

hello from italy

Felix Laate

Saturday 05 July 2008 8:20:00 am

Hi Mattia,

check out http://pubsvn.ez.no

Felix

Publlic Relations Manager
Greater Stavanger
www.greaterstavanger.com

H-Works Agency

Monday 07 July 2008 5:05:22 am

After some research on this specific topic it is not possible to POST variables on a different domain with redirection using PHP (cross-domain post redirection).

Due to browser security policy this redirection (using header 307) would open a warning window on most browser.

For those interested this is the header to redirect :

header("Location: URL", TRUE, 307);

At the end if you are stuck with the POST method, as in most payment gateway, you can only use en dirty javascript function that would auto-submit the form :(

EZP is Great

*- pike

Monday 07 July 2008 4:49:57 pm

> After some research on this specific topic it is not possible to POST variables on a
> different domain with redirection using PHP (cross-domain post redirection).

not with header 307 indeed:

> http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
> If the 307 status code is received in response to a request other than GET or HEAD,
> the user agent MUST NOT automatically redirect the request unless it can be
> confirmed by the user

I think you want a 303 :

> This method exists primarily to allow the output of a POST-activated script to redirect
> the user agent to a selected resource.

But a 'normal' header 302 should work fine as well ?

Honestly, I don't think it's very pretty though. You are trying to have php receive a post request, perform another request in itself, and then *simulate* the response of that request to the client. Is there really no way you can force the original client to make the request that php now would make internally ?

*-pike

---------------
The class eZContentObjectTreeNode does.

H-Works Agency

Wednesday 09 July 2008 4:30:46 am

Hello,

Thanx for your response but i am still stucked on this function.

I manage to do all kinds of post with php (cURL, fsocket, header()...etc) but none of them can actually redirect while posting.

I am just trying to POST variables to another domain with redirection (like an html form with action="otherdomain/page" would do.

Practically what would be the php command ?

Thanx in advance.

EZP is Great

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.