tags:

views:

155

answers:

4

I have a submit form with method POST, I want to write a script that can automatically submit this form, the reason why I need this is for testing purposes. I need a lot of data in little time in order to test a search based on those form fields, and I do not have time to mannulally do this. Is this possible?

A: 

Let PHP fill the form with data and print out a Javascript that posts the form, PHP can not post it on it's own thou.

Björn
Why the down vote? He explicit asks for a script to _post a form_, which PHP cannot accomplish.
Björn
Any downvoting is likely due to the fact that you said PHP cannot do what the user was asking, but it has libraries which can accomplish what was asked, as other answers have pointed out. I suggest that if you don't want downvotes, modify your answer somehow or delete it.
PTBNL
Well, no. I don't care if I get downvotes if I should be wrong, but what the PHP-libraries (like Snoopy) does is to simply post data with POST to a specified PHP document. That's not the same as posting a form... As I understood his initial question was to submit a HTML-form, and not to simply post data to a page.
Björn
A: 

You can use php.net/curl to send POST requests with PHP.

reko_t
+5  A: 

You can use curl to simulate form submit.

// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/script.php");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, true ); //enable POST method

// prepare POST data
$post_data = array('name1' => 'value1', 'name2' => 'value2', 'name3' => 'value3');

// pass the POST data
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data );

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

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

Petr Peller
You've set curl_setopt($ch, CURLOPT_POST, true ); twice.
luiscubal
+1  A: 

if your not comfortable using curl you could use a php library called snoopy that simulates a web browser. It automates the task of retrieving web page content and posting forms.

<?php
/* load the snoopy class and initialize the object */
require('../includes/Snoopy.class.php');
$snoopy = new Snoopy();

/* set some values */
$p_data['color'] = 'Red';
$p_data['fruit'] = 'apple';

$snoopy->cookies['vegetable'] = 'carrot';
$snoopy->cookies['something'] = 'value';

/* submit the data and get the result */
$snoopy->submit('http://phpstarter.net/samples/118/data_dump.php', $p_data);

/* output the results */
echo '<pre>' . htmlspecialchars($snoopy->results) . '</pre>';
?>
solomongaby