2 Replies Latest reply on Jul 15, 2009 4:12 AM by vertmonkee

    Receiving POST data with PHP

    vertmonkee

      I am slowly building up a script that will send XML to a PHP script.

       

      The PHP script works fine if I just load in XML from a file but I am having problems when I try and send POST data to it form Flex.

       

      My mxml code is as follows

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">

       

          <mx:Script>
              <![CDATA[
                  import mx.collections.XMLListCollection;
                  import mx.controls.Alert;
                 
                  public function sendTheData():void {
                     
                      //Create some XML
                      var xmlToSend:XML =
                          <species>
                            <speciesInfo>
                              <commonName>Basking Shark2</commonName>
                              <scientificName>Sharkus Baskius2</scientificName>
                              <defaultImage width="600" height="400">baskshark2.jpg</defaultImage>
                            </speciesInfo>
                            <speciesInfo>
                              <commonName>Dolphin2</commonName>
                              <scientificName>Flippinium Dolphius2</scientificName>
                              <defaultImage width="600" height="400">dolphin2.jpg</defaultImage>
                            </speciesInfo>
                          </species>

       

                      Alert.show("The XML has been created");

       

                      XML.ignoreWhitespace = true;
                      XML.prettyPrinting = false;
                     
                      var params:Object = new Object();
                      params.species = xmlToSend.toXMLString();               
                      sendXMLData.send(params);
                     
                  }
                 
              ]]>
          </mx:Script>
         
          <mx:HTTPService id="sendXMLData" url="http://localhost/xml_and_php/with_domdocument/reader.php" method="POST" useProxy="false"/>
          
          <mx:Button id="sendButton" click="sendTheData()" label="Send The Data" />
         
      </mx:WindowedApplication>

       

      And my PHP script is as follows

       

      <?php

       

      //Connect to the database
      $link = mysql_connect("localhost", "root", "new_password") or die(mysql_error());

       

      //Select the database
      mysql_select_db("flex_tests", $link) or die(mysql_error());

       

      //Create a new dom document
      $doc = new DOMDocument();

       

      //Load the xml
      //$doc->load('species.xml');

       

      $post_data = $_POST['species'];

       

      $species_info = $doc->getElementsByTagName("speciesInfo");

       

      foreach($species_info as $info) {
         
          $common_name_node = $info->getElementsByTagName("commonName");
          $common_name = $common_name_node->item(0)->nodeValue;
         
          $scientific_name_node = $info->getElementsByTagName("scientificName");
          $scientific_name = $scientific_name_node->item(0)->nodeValue;
         
          $sql = "INSERT INTO `species` (`commonName`, `scientificName`) VALUES ('".mysql_real_escape_string($common_name)."', '".mysql_real_escape_string($scientific_name)."');";
          //echo "<p>$sql</p>";
         
          $result = mysql_query($sql) or die(mysql_error());

      }


      ?>

       

      Can anyone offer insight into why the post data is not being read in.

       

      I have written the post data to a text file so it is definetly receiving it.

       

      Thanks for any help

        • 1. Re: Receiving POST data with PHP
          vertmonkee Level 1

          Sorry I posted php code that was missing a line. In the example givenn I didn't load the XML into the dom object.

           

          The php code should be

           

          <?php

          //Connect to the database
          $link = mysql_connect("localhost", "root", "new_password") or die(mysql_error());

          //Select the database
          mysql_select_db("flex_tests", $link) or die(mysql_error());

          //Create a new dom document
          $doc = new DOMDocument();

          //Load the xml
          //$doc->load('species.xml');

          $post_data = $_POST['species'];

          $doc->load($post_data);

          $species_info = $doc->getElementsByTagName("speciesInfo");

          foreach(
          $species_info as $info) {
              
             
          $common_name_node = $info->getElementsByTagName("commonName");
             
          $common_name = $common_name_node->item(0)->nodeValue;
              
             
          $scientific_name_node = $info->getElementsByTagName("scientificName");
             
          $scientific_name = $scientific_name_node->item(0)->nodeValue;
              
             
          $sql = "INSERT INTO `species` (`commonName`, `scientificName`) VALUES ('".mysql_real_escape_string($common_name)."', '".mysql_real_escape_string($scientific_name)."');";
             
          //echo "<p>$sql</p>";
              
             
          $result = mysql_query($sql) or die(mysql_error());
              
          }

          //Open the text file and dump the print_r of the post in it
          $myFile = "test.txt";

          $fh = fopen($myFile, 'w') or die("can't open file");

          //$stringData = "Bobby Bopper\n";

          $post_details = "";

          foreach(
          $_POST as $key => $value) {
              
             
          $post_details .= "$key = $value \n\n";
              
          }

          fwrite($fh, $post_details);

          //$stringData = "Tracy Tanner\n";

          //fwrite($fh, $stringData);

          fclose($fh);


          ?>

          • 2. Re: Receiving POST data with PHP
            vertmonkee Level 1

            The problem was coming in the loading of the XML in the PHP script.

             

            Where I had this

             

            $doc->load($post_data);

             

            I should have had

             

            $doc->loadXML($post_data);