Forum Moderators: coopster

Message Too Old, No Replies

Using PHP to automate submiting content to a form using POST

         

ntbgl

11:34 am on Jul 14, 2008 (gmt 0)

10+ Year Member



I need to automate submiting content to a form using the post method, and then get the content of the resulting page.

Is this possible using PHP?

mrscruff

2:07 pm on Jul 14, 2008 (gmt 0)

10+ Year Member



If you know what the post data is you may be able to create hearedrs with the relivant info, and send it to the result page and get its responce.

Other wise you can use javascript to submit a form.

rob7591

2:22 pm on Jul 14, 2008 (gmt 0)

10+ Year Member



Look up the libcurl functions for PHP:

[us.php.net...]

ntbgl

2:35 pm on Jul 15, 2008 (gmt 0)

10+ Year Member



Thank you very much. cURL is exactly what I needed.

I followed a tutorial and built my first script.

Unfortunately the tutorial only showed how to post one variable, and I need to post four.

The current script below seems only to post the last variable, how can I tweak this script so it posts all four.

<?php

$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"http://www.site.com/form/");
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,"var1=these");
curl_setopt($ch,CURLOPT_POSTFIELDS,"var2=are");
curl_setopt($ch,CURLOPT_POSTFIELDS,"var3=my");
curl_setopt($ch,CURLOPT_POSTFIELDS,"var4=variables");

$result=curl_exec($ch);
curl_close($ch);
print $result;

?>

eelixduppy

2:43 pm on Jul 15, 2008 (gmt 0)



This should work:

$post = array('var1' => 'these', 'var2' => 'are', 'var3' => 'my', 'var4' => 'variables');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

Try that

ntbgl

3:42 pm on Jul 15, 2008 (gmt 0)

10+ Year Member



Awesome, the array is exactly what I needed.

My script now looks like this:

<?php
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"http://www.site.com/form/");
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,array("var1"=>"these","var2"=>"are","var3"=>"my","var4"=>"variables"));

$data=curl_exec($ch);
curl_close($ch);
?>

What I was trying to do with the $data variables was to contain the html code of the resulting page, so I can widdel it down to grab the content I needed. All I seem to be getting is a number, while at the same time, the html of the page is showing up on the page I'm running the script on.

How can I have the resulting page's html be confined to a variable, and not outputted into the php page?

Also, do I need to do anything special if var1 ends up being the size of a long text document? Are there size limits for text being sent via POST.

Thanks