parsing XML with PHP4

Status
Not open for further replies.

caleb

New member
Jun 28, 2007
5
0
0
Okay, I've been lurking on WF for a couple weeks now and I hate that my first post is asking for help, but such is life.

I'm writing some scripts to submit leads to LeadPile. I have the form setup to send to itself where it validates the user's information, adds it to my db, then POSTs it to LeadPile and then receives a response from LeadPile in XML format. Here's a sample of the response

Code:
<?xml version="1.0"?>
<response>
  <status>accept</status>
  <id>123456789</id>
  <sale>
    <price>12.00</price>
    <redirect>http://redirect-example.com</redirect>
  </sale>
</response>

The response is stored in $result which I then pass to:

PHP:
$parser = xml_parser_create('UTF-8');
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 
xml_parse_into_struct($parser, $result, $vals, $index);

Okay, at this point I know the information is stored in $vals and $index. But how do I get it broken down where the values are stored in variables named after the node?
 


Hope this helps. :)

Code:
//$result = file_get_contents("xml.xml");
$parser = xml_parser_create('UTF-8');
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 
xml_parse_into_struct($parser, $result, $vals, $index); 

//Mods 
xml_parser_free($parser);

$status =  $vals['1']['value'];
$id =  $vals['2']['value']; 
$price =  $vals['4']['value'];  
$redirect =  $vals['5']['value']; 

echo "<pre>
Status:     $status 
ID:           $id
Price:       $price
Redirect:   $redirect: 
</pre>";
 
for simple xml the best is just regex the result,that's the lowest overhead.

$regex = "/<status>(.+?)<.+?<id>(.+?)<.+?<price>(.+?)<.+?<redirect>(.+?)</m";
preg_match($regex,$result,$matches);
$status = $matches[1];
$id = $matches[2]
$price = $matches[3];
$redirect = $matches[4];

I didn't test that regex but that should be really close.
 
First off, thanks to all of y'all for helping me. Here's an update on where I'm at with this.

Some of the solutions that where suggested didn't work since I'm not dealing with a file, but rather an array. I had this problem when I googled for solutions. Seems everyone is dealing with files. I may eventually give up and save the array to a file and then open it, parse it, close it, and delete it. But it really seems inefficient so I'm trying to avoid that.

Some of the solutions didn't work because I left out a few things concerning the XML that gets returned. If the status value is "reject", then the next node is errors with child node of error (of which there could be more than one). So I can't assume that the second node will always be the same. I'm wondering if a simple if...then would work here.

So, I'm going back through the answers and seeing if I can tweak them and get this thing working.

Thanks again.
:bowdown: I'm not worthy
 
Status
Not open for further replies.