Page 1 of 1

How to submit forms on other sites

Posted: Sun Feb 25, 2007 6:03 pm
by psychotomus
How can I submit forms in php that are on other sites. for example, have a script that will fill out this post form and submit it.

Posted: Sun Feb 25, 2007 6:31 pm
by Kieran Huggins
you can either script it in a browser with javascript and frames (or ajax), or you could just POST to the form's destination itself with CURL.

Posted: Sun Feb 25, 2007 6:55 pm
by psychotomus

Code: Select all

<?php

   $post_data = array();
   
   $post_data['pictures[0]'] = "@cat.jpg";
   $post_data['pictures[1]'] = "@dog.jpg";
   

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "http://my.domain.com/my_url.php" ); 
   curl_setopt($ch, CURLOPT_POST, 1 );
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   $postResult = curl_exec($ch);

   if (curl_errno($ch)) {
       print curl_error($ch);
   }
   curl_close($ch);
   print "$postResult";
?>
i found this on php.net. is this how its done?

Posted: Mon Feb 26, 2007 12:36 pm
by psychotomus
I'm having troubles with this code.


this is my test1.php file

Code: Select all

<?php 

   $post_data = array(); 
    
   $post_data['subject'] = "the subject"; 
   $post_data['message'] = "the message"; 
    

   $ch = curl_init(); 
   curl_setopt($ch, CURLOPT_URL, "http://www.simsportal.net/test.php" ); 
   curl_setopt($ch, CURLOPT_POST, 1 ); 
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
   $postResult = curl_exec($ch); 

   if (curl_errno($ch)) { 
       print curl_error($ch); 
   } 
   curl_close($ch); 
   print "$postResult"; 
?>

this is my test.php file

Code: Select all

<?
include("includes.php");
if(isset($_POST['Submit']))
	send_mail("william@po2mob.com",$_POST['subject'],$_POST['message']);
?>
<form name="form1" method="post" action="">
  <input name="subject" type="text" id="subject">
  <br>
  <input name="message" type="text" id="message">
  <br>
  <input type="submit" name="Submit" value="Submit">
</form>
send mail is included in includes.php. it doesnt send me no email. it just echo's the form.

Posted: Mon Feb 26, 2007 1:11 pm
by feyd
Compare the $_POST requirements of the test script against what you are sending the form. If you don't understand, add debugging output to your test script.

Posted: Mon Feb 26, 2007 1:15 pm
by psychotomus
what you mean compare them?

Posted: Mon Feb 26, 2007 1:19 pm
by feyd
What your cURL script is providing and your test script is requiring.

Posted: Mon Feb 26, 2007 1:25 pm
by psychotomus
im posting
subject and message and my test.php has a subject and message field if thats what your asking.

Posted: Mon Feb 26, 2007 1:38 pm
by feyd
Count up the references to $_POST in your test script.

Now var_dump($_POST) in the test script. Notice anything missing?

Posted: Mon Feb 26, 2007 1:42 pm
by psychotomus
the submit button? =)

Posted: Mon Feb 26, 2007 1:45 pm
by psychotomus
my new test1.php file
it dont think its posting cause i havn't received an email. if i fill the form out normally i get an email.

Code: Select all

<?php 


   $post_data = array(); 
    
   $post_data['subject'] = "the subject"; 
   $post_data['message'] = "the message"; 
   $post_data['Submit'] = "Submit"; 
    

   $ch = curl_init(); 
   curl_setopt($ch, CURLOPT_URL, "http://www.simsportal.net/test.php" ); 
   curl_setopt($ch, CURLOPT_POST, 1 ); 
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
   $postResult = curl_exec($ch); 

   if (curl_errno($ch)) { 
       print curl_error($ch); 
   } 
   curl_close($ch); 
   print "$postResult"; 
?>

Posted: Mon Feb 26, 2007 1:50 pm
by feyd
Your setting of CURLOPT_POSTFIELDS is incorrect.

Posted: Mon Feb 26, 2007 2:00 pm
by psychotomus
is it supose to be like this?

curl_setopt($ch, CURLOPT_POSTFIELDS, 'message=MESSAGE&subject=SUBJECT&Submit=Submit);

Posted: Mon Feb 26, 2007 2:04 pm
by feyd
psychotomus wrote:is it supose to be like this?

curl_setopt($ch, CURLOPT_POSTFIELDS, 'message=MESSAGE&subject=SUBJECT&Submit=Submit);
Only one way to find out for yourself..

Posted: Mon Feb 26, 2007 3:45 pm
by psychotomus
they both work. just took ahwile for it to send the mail.