views:

72

answers:

6

I have an online gateway which requires an HTML form to be submitted with hidden fields. I need to do this via a PHP script without any HTML forms (I have the data for the hidden fields in a DB)

To do this sending data via GET:

header('Location: http://www.provider.com/process.jsp?id=12345&name=John');

And to do this sending data via POST?

A: 

Use curl for this. Google for "curl php post" and you'll find this: http://www.askapache.com/htaccess/sending-post-form-data-with-php-curl.html.

Note that you could also use an array for the CURLOPT_POSTFIELDS option. From php.net docs:

The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.

svens
A: 

It would involve the cURL PHP extension.

$ch = curl_init('http://www.provider.com/process.jsp');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "id=12345&name=John");
curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1);  // RETURN THE CONTENTS OF THE CALL
$resp = curl_exec($ch);
Matt
A: 

Your going to need CURL for that task I'm afraid. Nice easy way to do it here: http://davidwalsh.name/execute-http-post-php-curl

Hope that helps

Glyn Mooney
A: 

In your case I would use CURL for the job. There you can accomplish the thing that you request.

http://php.net/manual/en/book.curl.php

There is a lot of documentation about it and I think you will find your solution there.

example:

//extract data from the post
extract($_POST);

//set POST variables
$url = 'http://domain.com/get-post.php';
$fields = array(
                        'lname'=>urlencode($last_name),
                        'fname'=>urlencode($first_name),
                        'title'=>urlencode($title),
                        'company'=>urlencode($institution),
                        'age'=>urlencode($age),
                        'email'=>urlencode($email),
                        'phone'=>urlencode($phone)
                );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);
Arto Uusikangas
use http-build-query instead of "url-ify the data for the post" ( http://be.php.net/manual/en/function.http-build-query.php ).
Schnalle
+1  A: 

You can't do this using PHP.

As others have said, you could use cURL - but then the PHP code becomes the client rather than the browser.

If you must use POST, then the only way to do it would be to generate the populated form using PHP and use the window.onload hook to call javascript to submit the form.

C.

symcbean
Well thats weird that cURL isnt the solution as I have used it myself to do exactly this. But I guess your solution works as well.
Arto Uusikangas
A: 

You have to open a socket to the site with fsockopen() and simulate a HTTP-Post-Request. Google will show you many snippets how to simulate the request.

Tobias P.