SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Central Illinois
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Can a script running by cron submit a form?

    I'm trying to work out a PHP script that will run one time via a cron job. The script will obviously have no user-interaction and won't see the light of day through a browser.

    The problem is, the purpose of the script is to submit a form, and I don't know how to do that without a user loading the script in a browser and clicking the "Submit" button. So my question is, can a PHP script be made to submit a form without user interaction, and without JavaScript?

    Incidentally, I'm not using this for spamming purposes or anything like that. I just need to submit one form, and unfortunately I won't be able to be near a computer at the particular time it needs to be submitted. So I wanted to hard-code the form values into a script and submit it automatically by cron at the proper time.

    Any ideas?

    Thanks,

    Matthew

  2. #2
    SitePoint Addict
    Join Date
    Nov 2001
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found a class that looks like it will let you send the right POST headers to the target page so it thinks the form has been submitted.

    Form submit class

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    MatthewHSE

    When you submit a form, your browsers just makes a HTTP request, i.e. sends several (kilo) bytes to taget host over network. Of course, your program can do something like this too. There are many tools and classes for this out there, but if you don't need any advanced functionality, you can do the same with few lines of code:

    PHP Code:
    // form fields and their values
    // feel free to change

    $vars = array(
        
    'firstname' => 'John',
        
    'lastname' => 'Doe',
    );

    // form is being sent to 
    // www.example.com/form.cgi

    $host "www.example.com";
    $url  "/form.cgi";

    // prepare post message body

    $body = array();
    foreach(
    $vars as $name => $value)
        
    $body[] = "$name=" urlencode($value);
    $body implode("&"$body);

    // prepare post message headers

    $post[] = "POST $url HTTP/1.1";
    $post[] = "Host: $host";
    $post[] = "Content-Type: application/x-www-form-urlencoded";
    $post[] = "Content-length: " strlen($body);
    $post[] = "Connection: Close";
    $post[] = "";
    $post[] = $body;

    $post implode("\r\n"$post);

    // connect to the host

    $sock fsockopen($host80$errno$errstr);
    if (!
    $sock)
       die(
    "fsockopen said: $errstr ($errno)");

    // send the form

    fwrite($sock$post);

    // close connection

    fclose($sock); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •