Jump to content

Multidimesional Arrays with Amazon script


bobbox

Recommended Posts

:) Hi

 

My PHP skills are very rudimentary, i understand arrays but i don't understand how THIS array is indexed

 

Basically i have an XML document that is parsed from Amazon. I can access the XML tags using for eg. $item['ItemAttributes']['Author'] (inside a foreach loop where there are multiple items inside of ItemResponse->Items) But i can't seem to drill down to a third level. For example $item['OfferSumary']['Offers']['Currency']

 

This is the class that parses the xml and makes the array.

 

<?php

// tools.inc.php -- contains two helpful functions, XmlParser() and GetData()

 

// Parser originally by Torsten K?ter (torsten at jserver dot de) with some mods by me

 

// List elements as of the 2005-02-23 Amazon WSDL

$xml_list_elements = array(

"Accessory", "Actor","Argument","Artist","ASIN","AudioFormat","Author","BrowseNode","BrowseNodes",

"CameraManualFeatures","CartItem","ChildTransactionItems","Creator","Customer", "CustomerReviews",

"Customers","Director","Disc","EditorialReview","Element","Error","Feature","Feedback",

"Format","Header", "Information","Item", "Items","Language", "List","ListmaniaList","ListItem","Lists",

"Offer","OfferListing","Operation", "OperationInformation","Package","Parameter","PhotoFlashType",

"PictureFormat","Platform","ResponseGroup","ResponseGroupInformation","Review","SavedForLaterItem",

"SearchIndex","Seller","SellerListing","SellerListings","Sellers","Shipment","SimilarProduct",

"SpecialFeatures", "SupportedImageType","Track", "Transaction","TransactionItem","TransactionItemId",

"Transactions"

);

 

// Global error strings from XmlParser and GetData

$parser_error='';

$getdata_error='';

 

// returns associative array or false on error. If there's an error, the global $parser_error will

// contain the error details

function XmlParser($string)

{

    global $parser_error;

 

    $parser_error='';

    $values=array();

 

    // Create parser

    $p = xml_parser_create("UTF-8");

    xml_parser_set_option($p,XML_OPTION_CASE_FOLDING,false);

    xml_parser_set_option($p,XML_OPTION_SKIP_WHITE,true);

 

    // Parse into array structure

    $rc = xml_parse_into_struct($p,$string,$values);

 

    /* Check for Parsing Error */

    if (!$rc)

    {

        $errorcode = xml_get_error_code($p);

        $errstring = xml_error_string($errorcode);

        $byte= xml_get_current_byte_index($p);

        $parser_error = "XML PARSER ERROR: Error Code= $errorcode, Explanation= $errstring, Byte Number= $byte";

        xml_parser_free($p);

        return false;

    }

 

    xml_parser_free($p);

 

    // We store our path here

    $hash_stack = array();

 

    // This is our target

    $ret = array();

 

    foreach ($values as $val) {

 

        switch ($val['type']) {

            case 'open': // Start array structure

            array_push($hash_stack, $val['tag']);

            $valarg= (isset($val['attributes'])) ? $val['attributes'] : null;

            $ret = composeArray($ret, $hash_stack, $valarg);

            break;

 

            case 'close': // All done with this element

            array_pop($hash_stack);

            break;

 

            case 'complete':

            array_push($hash_stack, $val['tag']);

            $valarg=(isset($val['value']))? $val['value'] : null;

            // handle all attributes except those in 'open' container tags

            if (isset($val['attributes'])) {

                $temparr=array($val['tag'] => $valarg);

                $valarg=array_merge($val['attributes'], $temparr);

            };

            $ret = composeArray($ret, $hash_stack, $valarg);

            array_pop($hash_stack);

            break;

 

            default:

            // Ignoring CDATA type

        }

    }

 

    return $ret;

}

 

function &composeArray($array, $elements, $value)

{

    global $xml_list_elements;

 

    // Get current element

    $element = array_shift($elements);

 

    // Does the current element refer to a list?

    if (in_array($element,$xml_list_elements))

    {

        // Are there more elements?

        if(sizeof($elements) > 0)

        {

            $array[$element][sizeof($array[$element])-1] = &composeArray($array[$element][sizeof($array[$element])-1], $elements, $value);

        }

        else // It's an array

        {

            $size = (isset($array[$element]))?  sizeof($array[$element]) : 0;

            $array[$element][$size] = $value;

        }

    }

    else

    {

        // Are there more elements?

        if(sizeof($elements) > 0)

        {

            $array[$element] = &composeArray($array[$element], $elements, $value);

        }

        else

        {

            $array[$element] = $value;

        }

    }

 

    return $array;

}

 

 

 

// Returns the response as a string or false on error

function GetData($url, $timeout) {

    global $getdata_error;

 

    // Parse the URL into parameters for fsockopen

    $UrlArr = parse_url($url);

    $host = $UrlArr['host'];

    $port = (isset($UrlArr['port'])) ? $UrlArr['port'] : 80;

    $path = $UrlArr['path'] . '?' . $UrlArr['query'];

 

    // Zero out the error response

    $errno = null;

    $errstr = '';

    $getdata_error = '';

 

    // Open the connection to Amazon

    $fp = @fsockopen($host, $port, $errno, $errstr, $timeout);

 

    // Failed to open the URL

    if (!is_resource($fp)) {

        $getdata_error = "Fsockopen error number = $errno Details = $errstr";

        return false;

    }

 

    // Send an HTTP GET header and Host header

    if (!(fwrite($fp, 'GET '. $path .' HTTP/1.0' . "\r\n". 'Host: ' . $host . "\r\n\r\n"))) {

        fclose($fp);

        $getdata_error = "Fwrite error. Could not write GET and Host headers";

        return false;

    }

 

    // Block on the socket port, waiting for response from Amazon

    if (function_exists('socket_set_timeout')) {

        @socket_set_timeout($fp, $timeout);

        socket_set_blocking($fp, true);

    }

 

    // Get the HTTP response code from Amazon

    $line = fgets($fp , 1024);

 

    if ($line == false){

        fclose($fp);

        $getdata_error = "Fgets error. Did not receive any data back from Amazon";

        return false;

    }

 

    // HTTP return code of 200 means success

    if (!(strstr($line, '200'))) {

        fclose($fp);

        $getdata_error = "HTTP error. Did not receive 200 return code from Amazon. Instead, received this: $line";

        return false;

    }

    // Find blank line between header and data

    do {

        $line = fgets($fp , 1024);

        if ($line == false) {

            fclose($fp);

            $getdata_error = "Fgets: did not receive enough data back from Amazon";

            return false;

        }

        if (strlen($line) < 3) {

            break;

        }

    } while (true);

 

    $xml='';

    // Fetch the data from Amazon

    while ($line = fread($fp, 8192))

    {

        if ($line == false) {

            fclose($fp);

            $getdata_error = "Fread: error reading data from Amazon";

            return false;

        }

        $xml .= $line;

    }

 

    fclose($fp);

    return $xml;

}

?>

 

What am i missing? I have no idea how this thing constructs the array and i need to be able to reach other child tags.

 

 

Link to comment
Share on other sites

Are you using php5. I wrote a php5 amazon bookstore, and using php 5 will save you a lot of marbles. If your host isn't running php5, get a new host. Life is much easier with php5. Send me a private message if you want to see my php. By the way, I have it all set up to run with mod rewrite, which is definately what you want to do to get the most SEO out of your amazon store. The script also uses a session to make sure that people that hit the back button don't lose their cart contents and that kind of good stuff.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.