Faqts : Business : Programming : Shopping For You : PHP : Common Problems

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

141 of 154 people (92%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

Is there a way to post a form to more than one php script!
How can I submit an http post method within a php program?
How to submit a form to various pages and get the output in different Iframes on a single page.

Feb 4th, 2010 05:39
Jens Clasen, Joe Bloggs, Shubhankar Banerjee, Brandon Prudent, Deepak Kumar Vasudevan, chris moss,


You cannot post a form to multiple reciptients using a HTML form and a 
web browser. To achieve this goal, you will need create a script 
receiving the form input and passing it on to the receiving hosts.
There are two possibilities:
The first thing you need to know is the transfer-method you want to use
for your script:
1. GET:
   As long as you have the URL_fopen_wrapper enabled in your 
   PHP-configuration you can simply use:
   <?php
     $output1=file("http://www.server.com/index.php?".$_SERVER
["QUERY_STRING"]);
     $output1=file("http://www.server2.com/index.php?".$_SERVER
["QUERY_STRING"]);
   ?>
   PHP allows for you to simply open any URL and pass parameters as if 
   it was a local file. 
2. POST:
   Here you'll find two ways of submitting a POST-request:
   o the cURL-Library provides a simple interface to emulate a browser:
     (see http://www.php.net/curl and the curl-manpage)
     <?php
       function HTTP_Post($URL,$data, $referrer="") {
         // parsing the given URL
         $URL_Info=parse_url($URL);
         // Building referer
         if($referer=="") // if not given use this script as referer
           $referer=$_SERVER["SCRIPT_URI"];
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL,$URL_Info["host"].$URL_Info
["path"]);
         curl_setopt($ch, CURLOPT_HEADER,1);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
         curl_setopt($ch, CURLOPT_REFERER, $referer );
         curl_setopt($ch, CURLOPT_POST,1);
         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
         $result=curl_exec ($ch);
         curl_close ($ch);
         return $result;
       }
       $output1=HTTP_Post("http://www.server1.com/script1.php",$_POST);
       $output2=HTTP_Post("http://www.server2.com/script2.php",$_POST);
     ?>  
    o since the cUrl-Library is an Extension the other way to submit
      a POST-request is doing so by hand:
      <?php
      function HTTP_Post($URL,$data, $referrer="") {
        // parsing the given URL
        $URL_Info=parse_url($URL);
        // Building referrer
        if($referrer=="") // if not given use this script as referrer
          $referrer=$_SERVER["SCRIPT_URI"];
        // making string from $data 
        $data_string=http_build_query($data);
        // Find out which port is needed - if not given use standard 
(=80)
        if(!isset($URL_Info["port"]))
          $URL_Info["port"]=80;
        // building POST-request:
        $request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
        $request.="Host: ".$URL_Info["host"]."\n";
        $request.="Referer: $referrer\n";
        $request.="Content-type: application/x-www-form-urlencoded\n";
        $request.="Content-length: ".strlen($data_string)."\n";
        $request.="Connection: close\n";
        $request.="\n";
        $request.=$data_string."\n";
        $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
        fputs($fp, $request);
        while(!feof($fp)) {
            $result .= fgets($fp, 128);
        }
        fclose($fp);
        return $result;
      }
      $output1=HTTP_Post("http://www.server1.com/script1.php",$_POST);
      $output2=HTTP_Post("http://www.server2.com/script2.php",$_POST);
      ?>
    Note: The third parameter is thought for making a recipient think
          the values came frome somebody else then they actually do!
          A common way to prevent other peopple doing lazy things with
          a web form is to check the referrer - though this does not
          help in every case as you can see.