Solved

PHP auto-fil and auto-submit html form

Posted on 2009-07-02
7
529 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 108

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 108

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
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.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

920 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

13 Experts available now in Live!

Get 1:1 Help Now