How to automatically submit a (POST) form from remote server?

10,942

Solution 1

You can't fill the form with php since php is a server-side language. You could fill in the form with javascript. There are some frameworks to do that (phantomjs, casperjs).

You can try to post the form data directly using curl in PHP.

<?php

$data = array(
    'submit_form' => 1,
    'field_name' => 'your value here',
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/post-url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$output = curl_exec($ch);
$info = curl_getinfo($ch);

curl_close($ch);

You can find the url by looking in the console of your browser:

form post

Your request might be blocked because it's coming from another server but you can give it a try. If you want to 'mimic' a normal visitor you can start with setting the user agent string, maybe modify the HTTP_REFERER as well.

curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36');
curl_setopt($ch, CURLOPT_REFERER, 'http://www.example.com/the-url-of-the-form');

Solution 2

$(document).ready(function(){
    $('#form1').submit(ajax);
})
function ajax(){
        $.ajax({
            url : 'inserir.php',
            type : 'POST',
            data : $('form').serialize(),
            success: function(data){
                $('#resultado').html(data);
            }
        });
        return false;
}
...
window.onload=function(){
    setInterval(ajax, 5000);
}
check the link for more JQuery AJAX auto submit form
Share:
10,942
cybergeek654
Author by

cybergeek654

Updated on June 04, 2022

Comments

  • cybergeek654
    cybergeek654 almost 2 years

    I am trying to automatically fill in a field and submit a form at another server. I do not have any control on the target page and server.

    I tried writing running php code with Snoopy class to do the job, but it did not work (description here)

    The page that I am trying to fill in and submit automatically is: http://example.com/?page=a and the source of the page is as follows:

    <form action="" method="post" class="horizontal-form" role="form" >
    <input type="hidden" name="submit_form" value="true" />
    <input type="text" name="field_name" class="form-control" value="" >
    <button type="submit" class="btn"><i class="icon-ok"></i> Send</button>
    </form>
    

    Any idea how I can fill in the "field_name" automatically using a script/php code from my own site and automatically submit this form? I guess I can use CURL to do it as well, but I am novice and don't know how. :(

    Thank you for your help.

  • cybergeek654
    cybergeek654 almost 9 years
    would this work even though I do NOT have control over the target website with the form in it?
  • cybergeek654
    cybergeek654 almost 9 years
    Thanks for the post. I tried this code, including the useragent and referrer as well. Could not get it to work. Maybe I need to set the cookies?! is this correct syntax to set cookies in CURL? curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: PHPSESSID=cookie1; PHPSESSID2=cookie2; ")); or this one: 4)curl_setopt($ch, CURLOPT_COOKIE, "PHPSESSID=cookie1; PHPSESSID2=cookie2; ");
  • Gerard
    Gerard almost 9 years
    What doesn't work exactly, are you able to make the request? What's te response?
  • cybergeek654
    cybergeek654 almost 9 years
    After running the code, the results is the html code of the target page, exactly as it is on the target website. This is not good, because if the POST was successful, the result page should have contained one div section saying that "field_name" was incorrect format or that submission was successful.
  • Jobin
    Jobin almost 6 years
    No it will not due to cross domain policy!