SiteCrafting Blah Blah Blog

Jan. 19, 2007 at 4:52pm

Emulating Form POST in PHP

There's small choice in rotten apples.

Recently, I had a need to POST a form, using PHP script, to a remote url.  As I started to research a solution, I soon realized there were very few limited options.

Here's the situation, a customer has added a number of items to his shopping cart and ready to submit the final order. When the submit button is pressed, the PHP script processees the order locally, and then the next step is to POST the shopping cart to a remote url, which I have no control over. This system is very similar to integrating a website with PayPal.

Here are my choices

Javascript

The shopping cart is processed locally. Afterwards the php script creates an html page with a hidden POST form with the needed values. We add Javascript to submit the form when the page loads.

Comment

Unfortunately, this was not an option, since there's a strong possibility that some customers will have javascript disabled.

cURL and Header("Location:  http://www.theremoveurl.com")

In this example, cURL (or fsockopen) is used to post the variables to the remote. After the cURL operation is complete use Header("Location: url") to redirect to the website.

It would also be possible to get the response, which should be a html and echo() or print() it to the customer's browser.

Comment

This was not an option since the remote creates a session and redirects to an unknown URL after the form has been submitted. There's no way to transfer the session information (well maybe) to the customer's browser and then redirect the browser to the final url by using the header information from the cURL response.

Intermediate Page

After the customer presses the submit button, the PHP script processes the order locally. Next, an intermediate page is used that says something like, "Thank you, your order has been submitted. Please press the OK button to continue on to your next companies web site. If you don't, then your company will not know about the order you just placed and bad things might happen."

Comment

This was the best solution to the problem at hand.

Posted in Coding Techniques, From the Workbench, PHP by Ken Foubert

Comments (4)

Very interesting. I think you were wise in choosing the option you did. One suggestion might be to make the "intermediate" page seem more like another step in the process. By saying "your order has been submitted," it implies the process is functionally over. By making this page seem like the next-to-last step in the process, you could avoid the slightly intimidating "bad things might happen" message and ensure the user goes through with the final step.
1 | Left by Bernie Zimmermann | Jan. 20, 2007 at 1:32am


Brian says:

Great point Bernie...and it will be part of the process almost like a page that shows all contents of the order for confirmation prior to submission. Upon submit they will be returned to their internal application.
2 | Jan. 20, 2007 at 7:36am


Joe says:

I see the intermediate page step used by quite a few different sites, particularly in the posting of payments. It's helpful both as an additional processing step as well as preventing a lot of errors by presenting all the data entered on the previous page in a readable format and allowing the user to go back and correct any mistakes, or carry on to final submission.
3 | Jan. 20, 2007 at 5:18pm


Ken says:

Thanks Bernie for the tip. Joe, glad to hear that method is being used by other sites.

FYI If you wish to see how to send form post using php's fsockopen check this link, http://www.zend.com/zend/spotlight/mimocsumissions.php. To see how cURL does it simply check out the cURL library at php.net, http://us2.php.net/manual/en/ref.curl.php
4 | Jan. 22, 2007 at 2:46pm


Remember me
Name: Email: URL: Comment: *   No HTML, http:// will auto-link
* required    Comment Guidelines