vik
vik

Reputation: 772

Extending Zend_Service for Amazon BrowseNodes, Ancestors are not processed

The Zend Service for Amazon does not provide BrowseNodes, so I decided to extend it. The Zend code can be found at http://framework.zend.com/manual/de/zend.service.amazon.html.

My code below works almost but Just Ancestors are not processed. I wonder if there is an error in the wsdl or the xpath. I am trying to debug, but or just processing Ancestors. BrowseNodeId, Name, IsCategoryRoot, Children are processed but Ancestors array is always empty. The xpath query is able to find Children, but not Ancestors.

Please find the Code below. And a part of the xml

Here is the anatomy of the wsdl for the XML extract of a ItemLookup http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/AnatomyofaWSDL.html

The partial of xml response

<BrowseNodes>
    <BrowseNode>
        <BrowseNodeId>10605</BrowseNodeId>
        <Name>Education</Name>
        <Children>...</Children>
        <Ancestors>
            <BrowseNode>
                <BrowseNodeId>21</BrowseNodeId>
                <Name>Education & Reference</Name>
                <Ancestors>
                    <BrowseNode>
                        <BrowseNodeId>1000</BrowseNodeId>
                        <Name>Subjects</Name>
                        <IsCategoryRoot>1</IsCategoryRoot>
                        <Ancestors>
                            <BrowseNode>
                                <BrowseNodeId>283155</BrowseNodeId>
                                <Name>Books</Name>
                            </BrowseNode>
                        </Ancestors>
                    </BrowseNode>
                </Ancestors>
            </BrowseNode>
        </Ancestors>
    </BrowseNode>
</BrowseNodes>

How I am extending Zend_Service_Amazon_Item for BrowseNodes

class Zend_Service_Amazon_Item
{

    /**
     * @var array
     */
    public $BrowseNodes = array();

    public function __construct($dom) {

        // .... the code for the other items

        // .... for browsenode
        $result = $xpath->query('./az:BrowseNodes/*', $dom);
        if ($result->length > 1) {
            /**
             * @see Zend_Service_Amazon_BrowseNode
             */
            require_once 'Zend/Service/Amazon/BrowseNode.php';
            foreach ($result as $r) {
                $this->BrowseNodes[] = new Zend_Service_Amazon_BrowseNode($r);
            }
        }
    }
}

I have created a new Class for the BrowseNodes

class Zend_Service_Amazon_BrowseNode
{
    /**
     * @var integer
     */
    public $BrowseNodeId;

    /**
     * @var string
     */
    public $Name;

    /**
     * @var boolean
     */
    public $IsCategoryRoot;


    /**
     * @var array BrwoserNodes
     */
    public $Ancestors = array();

    /**
     * @var string
     */
    public $Children = array();

    /**
     * Assigns values to properties relevant to BrowseNode
     *
     * @param  DOMElement $dom
     * @return void
     */
    public function __construct(DOMElement $dom)
    {

        $xpath = new DOMXPath($dom->ownerDocument);
        $xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2011-08-01');
        foreach (array('BrowseNodeId', 'Name') as $el) {
            $result = $xpath->query("./az:$el/text()", $dom);
            if ($result->length == 1) {
                $this->$el = (string) $result->item(0)->data;
            }
        }

        $result = $xpath->query('./az:IsCategoryRoot/text()', $dom);
        if ($result->length == 1) {
            $this->IsCategoryRoot = (bool) $result->item(0)->data ;
        }

        foreach (array('Children','Ancestors') as $el) {
            $result = $xpath->query("./$el/*", $dom);
            if ($result->length > 1) {
                foreach ($result as $r) {
                    array_push($this->$el, new Zend_Service_Amazon_BrowseNode($r));
                }
            }
        }
    }
}

Upvotes: 1

Views: 325

Answers (1)

vik
vik

Reputation: 772

I've found the answer, hope it helps other's too.

There are two errors:

  1. I don't know what az: stands for but it is needed. Instead of

        $result = $xpath->query("./$el/*", $dom);
    

    With "az:"

        $result = $xpath->query("./az:$el/*", $dom);
    
  2. I've been testing with only one Ancestor, so the argument in if is true, if I have a least two nodes in the result. According to the WSDL I could have Results with BrowseNodes, which have more than one Ancestor, but they must be rare or I was only looking for Items, which has only one Ancestor.

        if ($result->length > 1) {
    

    should be:

        if ($result->length > 0) {
    

Once again the corrected code in last part of Zend_Service_Amazon_BrowseNode

    foreach (array('Children','Ancestors') as $el) {
        $result = $xpath->query("./az:$el/*", $dom);
        if ($result->length > 0) {
            foreach ($result as $r) {
                array_push($this->$el, new Zend_Service_Amazon_BrowseNode($r));
            }
        }
    }

Upvotes: 0

Related Questions