-1

I need some help figuring out why I can't use $_POST['status'] with simplexml_load_string.

I am posting some data to a php page, then reading in $_POST['status']. The xml located inside $_POST['status'] then needs to be read into php's simplexml_load_string so I can parse the xml into an object.

If I hard code the simplexml_load_string() like this:

$xml = simplexml_load_string('<?xml version="1.0"?>
                                <BackgroundReports userId="" password="" databaseset="">
                                  <BackgroundReportPackage>
                                    <ReferenceId>1|9</ReferenceId>
                                    <OrderId>107284</OrderId>
                                    <ScreeningStatus>
                                      <OrderStatus flag="FALSE">x:partial</OrderStatus>
                                    </ScreeningStatus>
                                    <Screenings>
                                      <Screening type="credit">
                                        <ScreeningResults type="result" mediaType="html" resultType="report">
                                          <InternetWebAddress><![CDATA[https://somewhere.com]]></InternetWebAddress> 
                                        </ScreeningResults>
                                      </Screening>
                                    </Screenings>
                                  </BackgroundReportPackage>
                                </BackgroundReports>');

And dump it with var_dump($xml) The output is:

XML

object(SimpleXMLElement)#3 (2) {
  ["@attributes"]=>
  array(3) {
    ["userId"]=>
    string(0) ""
    ["password"]=>
    string(0) ""
    ["databaseset"]=>
    string(0) ""
  }
  ["BackgroundReportPackage"]=>
  object(SimpleXMLElement)#4 (4) {
    ["ReferenceId"]=>
    string(3) "1|9"
    ["OrderId"]=>
    string(6) "107284"
    ["ScreeningStatus"]=>
    object(SimpleXMLElement)#5 (1) {
      ["OrderStatus"]=>
      string(9) "x:partial"
    }
    ["Screenings"]=>
    object(SimpleXMLElement)#6 (1) {
      ["Screening"]=>
      object(SimpleXMLElement)#7 (2) {
        ["@attributes"]=>
        array(1) {
          ["type"]=>
          string(6) "credit"
        }
        ["ScreeningResults"]=>
        object(SimpleXMLElement)#8 (2) {
          ["@attributes"]=>
          array(3) {
            ["type"]=>
            string(6) "result"
            ["mediaType"]=>
            string(4) "html"
            ["resultType"]=>
            string(6) "report"
          }
          ["InternetWebAddress"]=>
          object(SimpleXMLElement)#9 (0) {
          }
        }
      }
    }
  }
}

However if I use the $_POST['status'] like this $xml = simplexml_load_string($_POST['status']); instead of hard coding, then simplexml_load_string does not work. I know that $_POST['status'] has all the same xml...I dumped it out and it is exactly the same as when I hardcode it as a parameter to simple_xml_string.

the output now becomes this when I try to use $_POST['status']:

XML

bool(false)

I need to figure out how to be able to use this with $_POST['status'] as the affiliate will only POST back to my php page.

5
  • Simple answer: the $_POST['status'] is NOT the same. Find out why, that's the harder part, can't do that from here, try to var_dump instead of echo it.
    – Wrikken
    Nov 9, 2012 at 0:22
  • I var_dumped...I've compared the output from var_dump and cut 'n pasted it directly into the code so it was hard coded...and it works...but the problem is I can't hard code it...I need it from the post variable...and that still does not work.
    – Ronedog
    Nov 9, 2012 at 0:30
  • Did you copy/paste from the source of the page or just the output in your browser?
    – Wrikken
    Nov 9, 2012 at 0:31
  • Are you doing the var_dump on a web page? Then do var_dump(htmlentities($_POST['status']));, otherwise the browser will interpret the XML as HTML.
    – Barmar
    Nov 9, 2012 at 0:47
  • php.net/simplexml_load_string - read about the return value of that function. FALSE means the string you load is not valid XML. That simple it is.
    – hakre
    Nov 9, 2012 at 1:19

2 Answers 2

0

Perhaps you have magic quotes enabled on your host. You can make a simple test form and POST to see.

<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
    if(get_magic_quotes_gpc()) {
        echo 'Magic quotes enabled, so GET,POST,REQUEST,COOKIE Strings are escaped';
        $status = stripslashes($_POST['status']);
    }else{
        $status = $_POST['status'];
    }

    print_r(simplexml_load_string($status));
}else{
?>
<h1>Test XML POST</h1>
<form method="POST" action="">
  <p><textarea rows="9" name="status" cols="36"></textarea></p>
  <p><input type="submit" value="Submit"></p>
</form>
<?php } ?>

Also adding error_reporting(E_ALL); wont hurt.

0

Thanks to all who helped me out. Here is what I found out. When I was "Posting" there was an extra line in the xml like this

"

<?xml version="1.0"?>bla bla
"

When I removed the blank line like this it all worked.

"<?xml version="1.0"?>bla bla
"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.