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 mannul开发者_运维问答ally do this. Is this possible?
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
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>';
?>
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.
You can use php.net/curl to send POST requests with PHP.
精彩评论