1

I'm using a python script from with in php to call some xml date from a c# server. When I pass it through simplexml_load_string to parse my data. The python script is called for a few different things, but for one of the calls it gives me

Warning: simplexml_load_string(): Entity: line 1: parser error : StartTag: invalid element name

My PHP is:

$output = exec("python Auth.py -M authenticate -p 17df30b0-37c1-4ebf-b45e-39353bd971a9 -P 5b9abe609e1ab31555562dd959fc050d");
$xml = simplexml_load_string($output, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);

My Python is:

params = urllib.urlencode(data);
conn = httplib.HTTPConnection("localhost", 8003);
conn.request("POST", path, params)
response = conn.getresponse()
print response.read();

And my XML is:

<?xml version="1.0"?>
<ServerResponse>
  <Result>Success</Result>
  <Token>25027fc3-e3c9-46a8-a1b7-5d217fecfe2c</Token>
</ServerResponse>

I can run my Python script from my Ubuntu command line just fine and I wrote the output into a PHP file as XML data and it worked fine on parsing it.

So I don't know what's wrong. Can anybody help?

3
  • please add the xml with your question. Commented Jan 29, 2017 at 8:04
  • Have you checked value of $output? Commented Jan 29, 2017 at 8:12
  • Yes, var_dump 'string(17) "" ' Commented Jan 29, 2017 at 8:18

1 Answer 1

0

You are only capturing the last line of the Python print when assigning exec() to a variable. And this last line is not sufficient to create a SimpleXMLElement with simplexml_load_string.

Instead, pass the output stream into an array using the additional output and return_var arguments of PHP's exec(). Once you receive the $output array, iterate through it to build an XML string:

exec("python Auth.py -M authenticate -p 17df30b0-37c1-4ebf-b45e-39353bd971a9" .
     " -P 5b9abe609e1ab31555562dd959fc050d", $output, $return_var);

$strXML = '';    
foreach ($output as $line) {
    $strXML = $strXML . $line;
}

$xml = simplexml_load_string($strXML, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that had fixed the 'simplexml_load_string' but now I'm having a hard time getting the 'array[]' 'key:vaule'
To my last comment, I needed to check for "result" and "Result"

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.