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.

eZ debug

Timing: Jan 18 2025 19:35:45
Script start
Timing: Jan 18 2025 19:35:45
Module start 'layout'
Timing: Jan 18 2025 19:35:45
Module start 'content'
Timing: Jan 18 2025 19:35:46
Module end 'content'
Timing: Jan 18 2025 19:35:46
Script end

Main resources:

Total runtime0.8443 sec
Peak memory usage4,096.0000 KB
Database Queries106

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0061 588.0469152.6406
Module start 'layout' 0.00610.0029 740.687539.4766
Module start 'content' 0.00910.8335 780.1641901.8438
Module end 'content' 0.84260.0016 1,682.007833.3750
Script end 0.8442  1,715.3828 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00320.3752160.0002
Check MTime0.00130.1574160.0001
Mysql Total
Database connection0.00120.140210.0012
Mysqli_queries0.729986.45221060.0069
Looping result0.00100.11301040.0000
Template Total0.812696.220.4063
Template load0.00210.245220.0010
Template processing0.810596.000620.4053
Template load and register function0.00010.009210.0001
states
state_id_array0.00080.098010.0008
state_identifier_array0.00070.077620.0003
Override
Cache load0.00180.21071070.0000
Sytem overhead
Fetch class attribute can translate value0.00040.047870.0001
Fetch class attribute name0.00100.1173210.0000
XML
Image XML parsing0.00270.322570.0004
class_abstraction
Instantiating content class attribute0.00010.0066300.0000
General
dbfile0.00170.1959440.0000
String conversion0.00000.000840.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
14content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
16content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
23content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
6content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
4content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 65
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs