How to submit forms on other sites

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

How to submit forms on other sites

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post 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?
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

what you mean compare them?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What your cURL script is providing and your test script is requiring.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

im posting
subject and message and my test.php has a subject and message field if thats what your asking.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Count up the references to $_POST in your test script.

Now var_dump($_POST) in the test script. Notice anything missing?
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

the submit button? =)
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post 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"; 
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your setting of CURLOPT_POSTFIELDS is incorrect.
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

is it supose to be like this?

curl_setopt($ch, CURLOPT_POSTFIELDS, 'message=MESSAGE&subject=SUBJECT&Submit=Submit);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

they both work. just took ahwile for it to send the mail.
Post Reply