Elitmiar
Elitmiar

Reputation: 36839

Submitting a form using PHP

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?

Upvotes: 1

Views: 1855

Answers (4)

Gabriel Solomon
Gabriel Solomon

Reputation: 30035

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>';
?>

Upvotes: 2

Petr Peller
Petr Peller

Reputation: 8826

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

Upvotes: 5

reko_t
reko_t

Reputation: 56430

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

Upvotes: 0

Bj&#246;rn
Bj&#246;rn

Reputation: 29381

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.

Upvotes: 0

Related Questions