SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Michigan
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question XML Parse with PHP Not Working Correctly

    I am trying to use PHP to parse some XML from a Shopping.com API and the target URL for the XML file is here and the script page is located here.

    The code I am using is the following:
    PHP Code:
    echo "<div id='total_num_of_matches_container'><div id='total_num_of_matches'>YOU FOUND ".$parser->document->categories[0]->category[0]->items[0]->tagAttrs['matchedItemCount']." MATCHES</div>";

    foreach(
    $parser->document->categories[0]->category[0]->attributes[0]->attribute as $attributes) {
        echo 
    "<div id=total_matches_container><div id=total_matches><div id=total_num_of_matches>
    Find by&nbsp;"
    .$attributes->name[0]->tagData;"</div>";

    echo 
    "<br><span class='result_list'><a href='".$attributes->attributeValues[0]->attributeValue[0]->tagAttrs['id']."'>".$attributes->attributeValues[0]->attributeValue[0]->name[0]->tagData."</a></span></div></div>";
        

    But as you can see, only the attributes->name appears and not that of any other values. I'm almost 100% sure I have the hierarchy correct, but I cannot figure out why the rest of it isn't parsing.

    Can anyone help? Thanks!
    Code CSS:
     

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    What parser are you using as it looks like a custom one (based on SimpleXML?)?

    A basic example, using SimpleXML, would look like:
    PHP Code:
    <?php header('Content-Type: text/plain; charset=utf-8'); error_reporting(E_ALL E_STRICT);

    $url 'http://sandbox.api.shopping.com/publisher/3.0/rest/GeneralSearch?apiKey=authorized-key&trackingId=7000610&keyword=Desktops&numAttributes=5&numAttributesWithValues=5';

    $document = new SimpleXMLElement($urlNULLTRUE);

    $category    $document->categories->category;
    $match_count $category->items['matchedItemCount'];
    $attributes  $category->attributes->attribute;

    echo 
    "You found $match_count matches.\n";
    foreach (
    $attributes as $attribute)
    {
        
    $value $attribute->attributeValues->attributeValue;
        
    printf("Find by %-20s -> %-16s (%s)\n"$attribute->name$value->name$value["id"]);
    }
    With the output being:
    Code:
    You found 4294 matches.
    Find by Price Range          -> Below $450       (price_range_0_450)
    Find by Installed RAM        -> At least 2.5 GB  (more_than_2_gb)
    Find by Brand                -> Lenovo           (brand_lenovo)
    Find by Hard Drive Capacity  -> At least 300 GB  (300_gb_or_more)
    Find by Processor Speed      -> At least 4.0 GHz (processor_speed__search__3_5_ghz_or_more)
    Note: In SimpleXML $blah->foo is the same as $blah->foo[0].
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Michigan
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well the parser is part of a larger script that parses other information, like pricing and such.

    In your script example, how do you make it show all the available attributes and not just the first one?

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    You'd just do another loop inside the foreach. E.g.

    PHP Code:
    foreach ($attributes as $attribute)
    {
        echo 
    "Find by {$attribute->name}\n";
        
    $values $attribute->attributeValues->attributeValue;
        foreach (
    $values as $value)
        {
            
    printf("  %-16s (%s)\n"$value->name$value["id"]);
        }

    Giving:
    Code:
    You found 4294 matches.
    Find by Price Range
      Below $450       (price_range_0_450)
      $450 - $700      (price_range_450_700)
      $700 - $1050     (price_range_700_1050)
      $1050 - $1480    (price_range_1050_1480)
      Above $1480      (price_range_1480_17090)
    Find by Installed RAM
      At least 2.5 GB  (more_than_2_gb)
      At least 2.0 GB  (installed_ram__search__2_gb)
      At least 1.5 GB  (1_5_gb)
      At least 1 GB    (installed_ram__search__1024_mb_or_more)
      At least 512 MB  (512_mb_or_more)
    Find by Brand
      Lenovo           (brand_lenovo)
      Hewlett Packard  (hewlett_packard)
      Dell             (dell)
      PIONEERPOS       (pioneer_pos)
      Acer             (acer)
    Find by Hard Drive Capacity
      At least 300 GB  (300_gb_or_more)
      At least 250 GB  (250___300_gb)
      At least 200 GB  (200___250_gb)
      At least 160 GB  (hard_drive_capacity__search__160___200_gb)
      At least 120 GB  (60_gb_or_more)
    Find by Processor Speed
      At least 4.0 GHz (processor_speed__search__3_5_ghz_or_more)
      At least 3.5 GHz (3_0_ghz_or_more)
      At least 3.0 GHz (2_5___3_0_ghz)
      At least 2.5 GHz (2_0___2_5_ghz)
      At least 2.0 GHz (1_5___2_0_ghz)
    Salathe
    Software Developer and PHP Manual Author.

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Michigan
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help, the script works good! I just have one issue.

    The final code is as follows:

    PHP Code:
    <?php    

    $url 
    'http://sandbox.api.shopping.com/publisher/3.0/rest/GeneralSearch?apiKey=authorized-key&trackingId=7000610&keyword='.$urlkeyword.'&numAttributes=4&numAttributesWithValues=5';



    $document = new SimpleXMLElement($urlNULLTRUE);



    $category    $document->categories[0]->category[0];

    $match_count $category->items[0]['matchedItemCount'];

    $attributes  $category->attributes[0]->attribute;

     

    echo 
    "<div id='total_num_of_matches_container'><div id='total_num_of_matches'>You found $match_count matches.\n</div>";

    foreach (
    $attributes as $attribute)
    {
        echo 
    "<div id=total_matches_container><div id=total_matches><div id=total_num_of_matches>Find by {$attribute->name}\n</div>";
        
    $values $attribute->attributeValues->attributeValue;
        echo 
    "  ";
        foreach (
    $values as $value)
        {
            
            
    printf("<span class='result_list'><a href=?attributeValue=%s&categories=".$_GET['categories']."&k=".$_GET['k'].">  %-16s</a></span><br> \n"$value["id"], $value->name);
        }
        echo 
    "</div></div>";



    echo 
    "</div>"?>
    In the code near the end, the script GET's the 'k' variable from the URL and codes that into an <a href>, but when the keyword in question is multiword (such as GPS+Navigation), the script only inputs GPS and anything after + is dropped. A one word keyword works fine. How do I fix this so the script will take a two word URL variable and keep it the way it is written, such as GPS+Navigation.

    Thanks again!

  6. #6
    SitePoint Zealot Kayarc's Avatar
    Join Date
    Sep 2009
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by angmi90 View Post
    Thanks for the help, the script works good! I just have one issue.

    The final code is as follows:

    PHP Code:
    <?php    

    $url 
    'http://sandbox.api.shopping.com/publisher/3.0/rest/GeneralSearch?apiKey=authorized-key&trackingId=7000610&keyword='.$urlkeyword.'&numAttributes=4&numAttributesWithValues=5';



    $document = new SimpleXMLElement($urlNULLTRUE);



    $category    $document->categories[0]->category[0];

    $match_count $category->items[0]['matchedItemCount'];

    $attributes  $category->attributes[0]->attribute;

     

    echo 
    "<div id='total_num_of_matches_container'><div id='total_num_of_matches'>You found $match_count matches.\n</div>";

    foreach (
    $attributes as $attribute)
    {
        echo 
    "<div id=total_matches_container><div id=total_matches><div id=total_num_of_matches>Find by {$attribute->name}\n</div>";
        
    $values $attribute->attributeValues->attributeValue;
        echo 
    "  ";
        foreach (
    $values as $value)
        {
            
            
    printf("<span class='result_list'><a href=?attributeValue=%s&categories=".$_GET['categories']."&k=".$_GET['k'].">  %-16s</a></span><br> \n"$value["id"], $value->name);
        }
        echo 
    "</div></div>";



    echo 
    "</div>"?>
    In the code near the end, the script GET's the 'k' variable from the URL and codes that into an <a href>, but when the keyword in question is multiword (such as GPS+Navigation), the script only inputs GPS and anything after + is dropped. A one word keyword works fine. How do I fix this so the script will take a two word URL variable and keep it the way it is written, such as GPS+Navigation.

    Thanks again!
    Does <?php var_dump($_GET['k']); ?> produce "GPS+Navigation"?
    Phoenix Arizona Web Design | info *at* kayarc.com | 602.633.2676

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Michigan
    Posts
    175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It dumps: string(14) "GPS Navigation"

  8. #8
    SitePoint Zealot Kayarc's Avatar
    Join Date
    Sep 2009
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by angmi90 View Post
    It dumps: string(14) "GPS Navigation"
    Ok so
    PHP Code:
    <?php
    $keyword 
    str_replace(' ''+'$_GET['k']);
    printf("<span class='result_list'><a href=?attributeValue=%s&categories=".$_GET['categories']."&k=".$keyword.">  %-16s</a></span><br> \n"$value["id"], $value->name); 
    ?>
    Phoenix Arizona Web Design | info *at* kayarc.com | 602.633.2676


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
  •