View Full Version : parsing file passed as a string

08-17-2012, 08:36 PM
I've been given code that returns an XML string. The string has one root element and all of the other elements are in the next level. I don't know how to process it, pull the data out and store in variables, for example.

I don't know what version of PHP. If necessary, how do I find out?

Thanks in advance!

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://xxxxxxxx.com/XML.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$data = $_POST;
$domain = $_SERVER['HTTP_HOST'];
$domain = str_replace('http://www.', '', $domain);
$domain = str_replace('http://', '', $domain);
$domain = str_replace('www.', '', $domain);
$data['domainName'] = $domain;
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);

// $output has the XML Data returned from my calculation

// Above: code provided to me. Below result: "Fatal Error: Call to undefined function"


$calculation = SimpleXMLElement($output);

The result is:

string(2095) "<?xml version="1.0" encoding="UTF-8"?>
<br />
<b>Fatal error</b>: Call to undefined function: simplexmlelement() in <b>/home/vg008web03/26/52/2905226/web/action.php</b> on line <b>20</b><br />

08-17-2012, 09:49 PM
You should find a tutorial to teach you how to handle XML files using PHP. Here's the first one I found on Google and it looks quite nice:


Also, the PHP manual at php.net is a great resource.


Good luck!

08-18-2012, 01:24 AM

$calculation = SimpleXMLElement($output);


$calculation = simplexml_load_string($output);

You might beable to use "new SimpleXMLElement($output)" but I've not tried. The point here is that creating a new object would need "new" infront of it.

08-18-2012, 04:42 AM
The guy who sent me the code was too busy to help me out so I really appreciate your reply. The guy thought my problem was that I don't have the correct version of PHP installed.

And contrary to what other people might think, I am trying to figure it out on my own. I had already tried both of your suggestions, (1) $calculation = simplexml_load_string($output); yields the same error. (2) new SimpleXMLElement($output); yields "Cannot instantiate non-existent class: simplexmlelement"

I've been looking for sample code that would parse the data but all I've found so far was code that parses a file and not the string.

08-27-2012, 06:19 AM
I found this somewhere else. It was suggested that I add this line to my .htaccess file:

AddHandler x-mapp-php5 .php

It didn't help.

08-27-2012, 10:48 AM
You must use simplexml_load_string($output) and I doubt you need the AddHandler.

What error is it giving with simplexml_load_string($output)? Give us the whole error text.

08-28-2012, 08:36 PM
Thanks, The error message is:
<b>Fatal error</b>: Call to undefined function: simplexmlelement() in <b>/home/vg008web03/26/52/2905226/web/action.php</b> on line <b>20</b><br />

...And that looks like is because I have PHP 4.4.9 installed. I'm afraid to upgrade to 5 because I don't want to have to fix anything that is already running fine.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum