Solved

PHP auto-fil and auto-submit html form

Posted on 2009-07-02
7
533 Views
Last Modified: 2013-11-10
I've got a weird idea here and was wondering how to go about it.  (I have no idea as to where to start.)

I've got a web-form that enters data into a mysql database when the user submits.  I would like to program a php script to automagically fill in the info *and* submit it so that I can automate a whole bunch of entries (1000 for example) so I can have a ton of random sample data to work with for some other scripts that will pull this data and possibly try to trend it.

Just so we're 100% clear, I know I can do it from the shell easy enough, but the thought struck me "I wonder if I could do it this other way?"  So this is more of a curiosity than anything.
0
Comment
Question by:Tony O'Byrne
  • 4
  • 3
7 Comments
 
LVL 6

Author Comment

by:Tony O'Byrne
ID: 24767841
Just to elaborate a little...  When I said that I know it could be done from the shell easy enough, I meant that I could do php/mysql via the shell.  What I'm really curious about is php actually sending the information and submitting it as if the user had done it.  I guess I would be trying to get php to send a "post" request to the server?

As I said, I've no idea where to start and my above terminology may be ... terminal.  Terminal terminology. :-)
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 24767843
Yes, you can post information to a script.  I use fsockopen() to do that.  I will post an example here for you in a moment. ~Ray
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 24768009
I'm having some 404 issues getting it to post to a script in my web root, but I think that is server-related.  The theory is sound, since I use this with PayPal all the time.

You would take the code example here, and place it into an iterative loop to post all the data you wanted.

Best regards, ~Ray
<?php // RAY_post_example_fsockopen.php
error_reporting(E_ALL);
 
// THE SITE WE WANT TO TALK TO - IN THIS CASE, TO MY SITE
$server_url  = "www.your.org";
$server_url  = $_SERVER["HTTP_HOST"];
 
// THE PAGE INSIDE THE WEB SITE
$request_uri = "/RAY_dump_superglobals.php";
 
// THE POST DATA THAT WE WANT TO SEND
$dat = array(
    'name' => 'joe',
    'mail' => 'joe@your.org',
    'word' => 'secret'
   );
 
// CONSTRUCT THE POST STRING URLENCODED AND SEPARATED BY AMPERSANDS
$postdata  = '';
foreach ($dat as $key => $val)
{
    $postdata .= '&' . $key . '=' . urlencode($val);
}
$postdata = ltrim($postdata, '&');
 
// CONSTRUCT THE HEADERS
$header = '';
$header .= "POST $request_uri HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($postdata) . "\r\n\r\n";
 
// SHOW WHAT WE ARE GOING TO DO
echo "<pre>\n";
echo "REQUEST DATA FOLLOWS:\n";
var_dump($server_url);
var_dump($request_uri);
var_dump($postdata);
var_dump($header);
echo "</pre>\n";
 
 
// OPEN THE SOCKET TO THE PAGE
$fp = fsockopen ($server_url, 80, $errno, $errstr, 30);
 
// TEST FOR VERIFICATION
if (!$fp)
{
	die("HTTP ERROR $server_url $request_uri fsockopen() FAILED ERRNO=$errno ERRSTR=$errstr");
}
 
// HTTP OPEN - WRITE HEADER AND REQUEST
fputs ($fp, $header . $postdata);
 
// HTTP OPEN - READ RESPONSE
$reply = '';
while (!feof($fp))
{
	$reply	.= fgets($fp, 1024);
}
 
// SEE WHAT THE FOREIGN SITE SAID
echo "<pre>\n";
echo htmlentities($reply);
echo "</pre>\n";
 
// FINIS
echo 'Done!';
?>

Open in new window

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 24768027
Here is the "catcher" script.
<?php // RAY_dump_superglobals.php
error_reporting(E_ALL);
 
// OPEN AN OUTPUT BUFFER
ob_start();
 
// SHOW THE SUPERGLOBALS
echo "\nGET ";   var_dump($_GET);
echo "\nPOST ";  var_dump($_POST);
echo "\nCOOKIE ";var_dump($_COOKIE);
 
// CAPTURE THE BUFFER
$vardump = ob_get_clean();
 
// SEND THE RESULTS
mail('you@your.org', 'VARDUMP', "$vardump");
 
// SHOW THE RESULTS
echo htmlentities($vardump);
?>

Open in new window

0
 
LVL 6

Author Comment

by:Tony O'Byrne
ID: 24768931
What's the "catcher" script for?
0
 
LVL 6

Author Closing Comment

by:Tony O'Byrne
ID: 31599373
Looks like this'll do. :-)  It gives me something to work off of which is what I was looking for.  If I have any further issues, I'll create a new thread.  Thanks for the help.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 24769018
The catcher script will receive the POST data and print it out for you.

Thanks for the points - it's a great question, ~Ray
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question