Web Hosting Talk







View Full Version : Post to Form from PHP and follow redirect


SimonT
07-30-2008, 09:48 PM
Is it possible to post to a remote form and then redirect the browser the output?

azizny
07-30-2008, 09:56 PM
Yes, use CURL with FOLLOW.

Peace,

SimonT
07-30-2008, 09:57 PM
Was just looking at CURL now (well curl command line) will look to see how it can be used in php

webcertain
07-31-2008, 03:36 AM
curl will probably require a php recompile, if its not there already.

SimonT
07-31-2008, 07:33 AM
Thanks for the help guys here is the code that I am working on, dragged from examples.


I get the following google error "Not Implemented
The server is unable to process your request" (I was just testing to google)

Also the location is still my http://localhost/curl/curltest.php
I was hoping it would have been http://www.google.com.au/search?hl=en&q=hello+world&btnG=Google+Search&meta=


<?php

$header[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: en-us,en;q=0.5";

$search = 'hello world';
$url = 'Http://www.google.com';
$curl = curl_init(); // Initialize the cURL handler
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9) Gecko/2008052906 Firefox/3.0');
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // Follow any redirects, just in case
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_URL, $url); // Set the URL
curl_setopt($curl, CURLOPT_POST, 1); // set POST method (to send our search)
curl_setopt($curl, CURLOPT_POSTFIELDS, 'q= .$search. &hl=en&btnG=Search&meta='); // What we will be searching for
curl_exec($curl); // Display page
curl_close($curl); // Close cURL handler

?>

SimonT
08-01-2008, 08:23 AM
Guys I dont think curl is going to work as curl is more about geting and posting I want the bowser to follow the redirect.

What else can I try ?

webcertain
08-01-2008, 08:24 AM
well, you need to grab the redirect results and pass them to the browser, shouldnt be that hard to get a result from curl ?

SimonT
08-01-2008, 06:59 PM
I want to submit from server A to a form on server B but I dont want the browser to end up showing a url on server A it should be now on a page on server B.

Any ideas on how to do this webcertain ? can i use a location header ?

webcertain
08-04-2008, 03:38 AM
well, that's how forms work naturally , you can post to any url you want in the action="" part.

SimonT
08-04-2008, 03:42 AM
I also want to check the entires as well as add 2 extra post commands and I want to do this in php not in front end java script.

webcertain
08-04-2008, 04:34 AM
hmm, that's getting awfully complicated.

What exactly are you wanting to do ?

SimonT
08-04-2008, 06:35 AM
web sorry I thought I had explained it all ready

1) User fills out the form;
2) Submits it to this script
3) PHP script validates information, adds more variables and sends it to the payment gateway
4) Sends refresh or redirect from payment gateway to browser

Jatinder
08-04-2008, 08:36 AM
Simon, the code you posted above won't work simply because Google's search form uses GET and not POST as you have done.

I think I have piece of code your can use somewhere on my PC. Will look for it and post it here.

webcertain
08-04-2008, 08:59 AM
hmm, you might find it easier to do this using sessions or form's posting data to each other (which is then included in hidden fields on the form - not ideal as its not secure to do that)

sessions is how I'd do it, store data in a session object, submit to script, let it update bits, let it generate a new form (could just be an 'ok/proceed to payment' button) and show confirmation page to user, and that page posts on to the payment gateway.

SimonT
08-04-2008, 10:50 PM
Hi guys sorry google was just an example I did find out that it needed a get request. It was the first site I tested with and soon found it needed a get request. Some one told me it is not possible to do this with curl as I need to redirect the browser to the second server does this mean I have to use sockets ?

Thanks for your help so far :-)

qbert220
08-05-2008, 07:12 AM
Do you want step 3 to be invisible to the user or could this step validate the input, add more then show a form with hidden fields to the user and a "Confirm" button that posts to the payment gateway?

Does step 4 rely on step 3 being done on the same browser (e.g. by setting a session cookie)? If so, then you cannot do step 3 on your server and step 4 in the user's browser.

sasha
08-05-2008, 08:17 AM
web sorry I thought I had explained it all ready

1) User fills out the form;
2) Submits it to this script
3) PHP script validates information, adds more variables and sends it to the payment gateway
4) Sends refresh or redirect from payment gateway to browser

What you are trying to do probably cannot be done or if it can be done it probably violates terms for using that payment gateway.

You can receive user input, add to it and send it to gateway. You can grab whatever gateway responds and show it back to user if you wish, but you probably cannot redirect user to the page gateway shows as result of the submition. Chances are that to display that page gateway requires some cookie to be set in user's browser and you cannot set it yourself because your site and gateway are not in the same domain.