Solved

PHP auto-fil and auto-submit html form

Posted on 2009-07-02
7
522 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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
What's the "catcher" script for?
0
 
LVL 6

Author Closing Comment

by:Tony O'Byrne
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now