SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Location
    San Antonio, TX
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Parsing an XML feed in PHP4

    I've got a form that I submit to a 3rd party whose server responds with XML similar to:

    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>
    I have the above XML information stored in $response, which I then do

    PHP Code:
    $parser xml_parser_create('UTF-8');
    xml_parser_set_option($parserXML_OPTION_SKIP_WHITE1); 
    xml_parse_into_struct($parser$response$vals$index); 
    xml_parser_free($parser); 
    Now, all I'm wanting to do is be able to call the values, such as status, id, price, etc. so that I can add them to a mySQL db, but so far I haven't been able to figure out how to do this. All the tutorials I've read deal with reading an xml file, but I'm not dealing with a file.

    Any help would be appreciated.

    -jeremy

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Documentation @ :
    http://us.php.net/manual/en/function...nto-struct.php

    The $index contains an index of xml keys.
    The $vals contain an array of values that correspond to the keys.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Location
    San Antonio, TX
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    Byron, as much as I loved the RTFM reference there, it doesn't help. I've read that page several times over the last couple days and I can't seem to wrap my head around it at this point.

    As much as I hate to ask, anyone have any newbie type tutorials for this part because everything I'm finding is just confusing me.... maybe I'm having a bad week or something.

    -jeremy

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do a var_dump on your $vals and $index and post the results back in this thread.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  5. #5
    SitePoint Addict buildakicker's Avatar
    Join Date
    Jun 2005
    Location
    NorCal
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could export a file, then delete it when finished taking necessary data from it.

    Just a quick thought.
    SKILEASES.COM - FREE rental listings!
    WILDFIREBLOG.COM - Wildland Fire microblog!

  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Location
    San Antonio, TX
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echo $result; outputs:
    Code:
    <?xml version="1.0"?>
    <response>
      <status>reject</status>
      <errors>
        <error>Your account is in test mode</error>
      </errors>
    </response>
    print_r(array_keys($index)); outputs:
    Code:
    Array ( [0] => RESPONSE [1] => STATUS [2] => ERRORS [3] => ERROR )
    print_r(array_values($index)); outputs:
    Code:
    Array ( [0] => Array ( [0] => 0 [1] => 5 ) [1] => Array ( [0] => 1 ) [2] => Array ( [0] => 2 [1] => 4 ) [3] => Array ( [0] => 3 ) )
    print_r(array_keys($vals)); outputs:
    Code:
    Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )
    print_r(array_values($vals)); outputs:
    Code:
    Array ( [0] => Array ( [tag] => RESPONSE [type] => open [level] => 1 ) [1] => Array ( [tag] => STATUS [type] => complete [level] => 2 [value] => reject ) [2] => Array ( [tag] => ERRORS [type] => open [level] => 2 ) [3] => Array ( [tag] => ERROR [type] => complete [level] => 3 [value] => Your account is in test mode ) [4] => Array ( [tag] => ERRORS [type] => close [level] => 2 ) [5] => Array ( [tag] => RESPONSE [type] => close [level] => 1 ) )
    -jeremy

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Location
    San Antonio, TX
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, granted parsing XML is new to me, but looking through the info in my above post, here's what I'm thinking.

    Shouldn't I be able to search through the $vals variable and find occurrences of status, id, price, redirect, error and then assign the next value to a variable by that name? So that when it finds the word status it creates $status which, in this case, would equal "rejected". That's all I'm wanting, so that I can then add them to my db.

    -jeremy

  8. #8
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dont split those arrays, show me the output from var_dump() for both.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  9. #9
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Location
    San Antonio, TX
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here ya go:

    print_r(var_dump($index));
    Code:
    array(4) { ["RESPONSE"]=>  array(2) { [0]=>  int(0) [1]=>  int(5) } ["STATUS"]=>  array(1) { [0]=>  int(1) } ["ERRORS"]=>  array(2) { [0]=>  int(2) [1]=>  int(4) } ["ERROR"]=>  array(1) { [0]=>  int(3) } }
    print_r(var_dump($vals));
    Code:
    array(6) { [0]=>  array(3) { ["tag"]=>  string(8) "RESPONSE" ["type"]=>  string(4) "open" ["level"]=>  int(1) } [1]=>  array(4) { ["tag"]=>  string(6) "STATUS" ["type"]=>  string(8) "complete" ["level"]=>  int(2) ["value"]=>  string(6) "reject" } [2]=>  array(3) { ["tag"]=>  string(6) "ERRORS" ["type"]=>  string(4) "open" ["level"]=>  int(2) } [3]=>  array(4) { ["tag"]=>  string(5) "ERROR" ["type"]=>  string(8) "complete" ["level"]=>  int(3) ["value"]=>  string(45) "Your account does not have open market access" } [4]=>  array(3) { ["tag"]=>  string(6) "ERRORS" ["type"]=>  string(5) "close" ["level"]=>  int(2) } [5]=>  array(3) { ["tag"]=>  string(8) "RESPONSE" ["type"]=>  string(5) "close" ["level"]=>  int(1) } }

  10. #10
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $vals[0]["tag"] = "RESPONSE";
    $vals[0]["type"] = "open";
    $vals[0]["level"] = 1;
    $vals[1]["tag"] = "STATUS";
    $vals[1]["type"] = "complete";
    $vals[1]["level"] = 2;
    $vals[1]["value"] = "reject";
    $vals[2]["tag"] = "ERRORS";
    $vals[2]["type"] = "open";
    $vals[2]["level"] = 2;
    $vals[3]["tag"] = "ERROR";
    $vals[3]["type"] = "complete";
    $vals[3]["level"] = 3;
    $vals[3]["value"] = "Your account does not have open market access";
    $vals[4]["tag"] = "ERRORS";
    $vals[4]["type"] = "close";
    $vals[4]["level"] = 2;
    $vals[5]["tag"] = "RESPONSE";
    $vals[5]["type"] = "close";
    $vals[5]["level"] = 1;

    function 
    getStatus$In_r ) {
        foreach ( 
    $In_r as $K ) {
            if ( 
    $K["tag"] == "STATUS" ) {
                
    $out $K["value"];
                return 
    $out;
            }
        }
    }

    echo 
    "Response = " .getStatus$vals ); 
    Yields : "Response = reject"
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •