SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: XML Feed an PHP

  1. #1
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Berlin
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question XML Feed an PHP

    Hi,

    I'm trying to integate following Amazon XML feed into my page:

    PHP Code:
     <Reviews>
             <
    AvgCustomerRating>4</AvgCustomerRating>
             <
    TotalCustomerReviews>4</TotalCustomerReviews>
             <
    CustomerReview>
                <
    Rating>5</Rating>
                <
    Summary>the best ...</Summary>
                <
    Comment>this bookpublished for the first time in 1978...</Comment>
             </
    CustomerReview>
             <
    CustomerReview>
                <
    Rating>1</Rating>
                <
    Summary>Pompous garbage</Summary>
                <
    Comment>This book is the most pompous garbage...</Comment>
             </
    CustomerReview>
             <
    CustomerReview>
                <
    Rating>5</Rating>
                <
    Summary>Most Important Book...</Summary>
                <
    Comment>Colin Rowe proposes...</Comment>
             </
    CustomerReview>
          </
    Reviews
    What I want to have in the end is something like:

    4 users rated with an average of 4 Points

    the best ... (5 Points)
    this book, published for the first time in 1978...

    Pompous garbage (1 Point)
    This book is the most pompous garbage...

    Most Important Book...(5 Points)
    Colin Rowe proposes...


    The Problem is, I can't find out, how to get this to work. (I think the DOM functions are not implementet on our webserver - it always says call to undefined function)

    I could get the content of singel (occuring) tags, like the price:

    PHP Code:
    $get_xml fopen("http://xml-eu.amazon.com/onca/xml3?t=$amazon_id&dev-t=$developer_token&AsinSearch=$isbn&type=heavy&f=xml&locale=de""r");
    while (
    $read fread($get_xml2048)) {
    $amazon_file .= $read;
    }
    fclose($get_xml);

    preg_match_all("/<OurPrice>(.*?)<\/OurPrice>/is"$amazon_file$amazonprice);

    echo 
    "".$amazonprice[1][0]."<br />\n"
    but I have no idea, how I could make the kind of listing I showed above

    I hope here is someone who can help me...

    Thanks
    Fl÷zen

  2. #2
    Ceci n'est pas Zoef Zoef's Avatar
    Join Date
    Nov 2002
    Location
    Malta
    Posts
    1,111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For a small xml file like this you might want to use xml_parse_into_struct as the simplest solution.

    Rik
    English tea - Italian coffee - Maltese wine - Belgian beer - French Cognac

  3. #3
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Berlin
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK thanks, that already brought me a bit futher. I have now following PHP Code:

    PHP Code:
    <?php

    class AmazonRezensionen {
        var 
    $Rating;
        var 
    $Summary;
        var 
    $Comment;
        
        function 
    AmazonRezensionen ($aa) {
            foreach (
    $aa as $k=>$v)
                
    $this->$k $aa[$k];
                }
    }

    function 
    readRezension($CustomerReview) {

    $data implode(""file($CustomerReview));
    $parser xml_parser_create();
    xml_parser_set_option($parserXML_OPTION_CASE_FOLDING0);
    xml_parser_set_option($parserXML_OPTION_SKIP_WHITE1);
    xml_parse_into_struct($parser$data$values$tags);
    xml_parser_free($parser);

    // loop through the structures
    foreach ($tags as $key=>$val) {
        if (
    $key == "CustomerReview") {
            
    $reviewranges $val;
            
    // each contiguous pair of array entries are the 
            // lower and upper range for each review
            
    for ($i=0$i count($reviewranges); $i+=2) {
                    
    $offset $reviewranges[$i] + 1;
                
    $len $reviewranges[$i 1] - $offset;
                
    $tdb[] = parseRezension(array_slice($values$offset$len));
                }
            } else {
                continue;
            }
        }
        return 
    $tdb;
    }

    function 
    parseRezension($amazonvalues) {
        for (
    $i=0$i count($amazonvalues); $i++) {
            
    $arez[$amazonvalues[$i]["tag"]] = $amazonvalues[$i]["value"];
            }
            return new 
    AmazonRezensionen ($arez);
    }

    $db readRezension("amazon.xml");
    The output with print_r($db); would look like this:

    PHP Code:
    Array

    (
        [
    0] => amazonrezensionen Object
            
    (
                [
    Rating] => 5
                
    [Summary] => the best to understand cities now and then
                
    [Comment] => this bookpublished for the first time in 1978...
            )

        [
    1] => amazonrezensionen Object
            
    (
                [
    Rating] => 1
                
    [Summary] => Pompous garbage
                
    [Comment] => This book is the most pompous garbage...
            )

        [
    2] => amazonrezensionen Object
            
    (
                [
    Rating] => 5
                
    [Summary] => Most Important Book on Urban Design Theory.
                [
    Comment] => Colin Rowe proposes a form of inclusive...
            )


    But how do I get this into what I mentioned above. I have now idea how this while construktion would need to look like???

    Please help

    Thanx so far,

    Fl÷zen

  4. #4
    SitePoint Addict
    Join Date
    Feb 2003
    Location
    Berlin
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, I thought, this would be an easy question to answer

    But I'll give it another try

    The Array is
    PHP Code:
    Array 


        [
    0] => amazonrezensionen Object 
            

                  [
    Rating] => 
                  
    [Summary] => the best to understand cities now and then 
                  
    [Comment] => this bookpublished for the first time in 1978... 
                  ) 

        [
    1] => amazonrezensionen Object 
            

                  [
    Rating] => 
                  
    [Summary] => Pompous garbage 
                  
    [Comment] => This book is the most pompous garbage... 
                  ) 

        [
    2] => amazonrezensionen Object 
            

                  [
    Rating] => 
                  
    [Summary] => Most Important Book on Urban Design Theory
                  [
    Comment] => Colin Rowe proposes a form of inclusive... 
                  ) 



    What I want to have as output on my HTML Page is:

    the best ... (5 Points)
    this book, published for the first time in 1978...

    Pompous garbage (1 Point)
    This book is the most pompous garbage...

    Most Important Book...(5 Points)
    Colin Rowe proposes...


    But I have no idea, how the while construction, or what ever I have to use for the array has to look like.

    Can sombody help me with this?

    Thanks
    Fl÷zen


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
  •