Topic: API Iterator Object

First of all, thank you for this framework.  I'm just trying to get used to it and using the API to integrate quickbooks with my web app.  My callback for...

$API->getItemByName('','_quickbooks_item_query_callback'); 

returns an Iterator object, and I'm wondering if the API allows receiving as an array, or what methods are available to parse this so as to populate an Item List table within my app. 

As far as testing, is there a way to mimic the QBWC and process in debug in real time?  Currently I am just queuing, processing with the QBWC and writing to and viewing the db log files. 

Thanks.

Re: API Iterator Object

Here is the code I am working with and attempting to get working:

function _quickbooks_item_query_callback($method, $action, $ID, &$err, $qbxml, $Items, $resource){
    global $dsn,$pm_conn,$mysql_str;
    
    $user = 'api';
    $source_type = QUICKBOOKS_API_SOURCE_WEB;
    $api_driver_dsn = $mysql_str;

    $source_dsn = 'http://quickbooks:test@localhost/qb/api_server.php';
    $api_options = array();
    $source_options = array('qbxml_version' => '10.0','qbxml_onerror' => 'stopOnError');
    $driver_options = array();

    if (!QuickBooks_Utilities::initialized($api_driver_dsn)){
        QuickBooks_Utilities::initialize($api_driver_dsn);
        QuickBooks_Utilities::createUser($api_driver_dsn, 'api', 'password');
    }

    $API = new QuickBooks_API($dsn, $user, $source_type, $source_dsn, $api_options, $source_options, $driver_options);
    $output = '';
    $ItemList = $API->getItemByName('');
    while ($Item = $ItemList->next()){
        QuickBooks_Utilities::log($dsn, 'Item: ' . print_r($Item, true)); 
        //EXTRACT LISTID, NAME, RATES, ETC to write to app db   
    }
    QuickBooks_Utilities::log($dsn, 'Received Item List: ' . print_r($Items, true));
}

Re: API Iterator Object

Why are you trying to "parse" an iterator? The iterator contains objects that you can use directly...

Can you clarify what you mean?

It doesn't really make sense to fetch data as an array, since the XML structure doesn't map particularly well to an array and it'd be a bit harder to use than the objects.

Re: API Iterator Object

The callback receives an Iterator Object, $Items.  This object contains a property of _list, which is protected and is an array of Item objects.  Because the data  is held within protected properties, I am unable to access it.  What I am looking for in this particular case is some example code of how one would iterate through this Iterator object using the appropriate child classes.  I changed the protected properties to public just to get some work done in the meantime, but this is clearly not how it was intended to be used. 

function _quickbooks_item_query_callback($method, $action, $ID, &$err, $qbxml, $Items, $resource){
    global $dsn,$sa;
    
    foreach($Items->_list as $Item){
        $x++;
        if(is_object($Item)){
            $new_arr['ListId'] = $Item->_object['ListID'];
            $new_arr['item_name'] = $Item->_object['Name'];
            $new_arr['invoice_item_active'] = ($Item->_object['IsActive'] == true) ? 1 : '0';
            $new_arr['fdx_superadmin'] = '1000';
            $new_arr['base_rate'] = $Item->_object['SalesOrPurchase Price'];
            list($s,$q) = $sa->db->buildWriteQuery('tbl_invoice_item',$new_arr);
            list($s,$r) = $sa->db->doQuery($q);
        }
    }
}

I would like to do the above the correct way, as you intended it to be done with with framework.  The only reason the above works is because I made the _list and _object properties public.

Re: API Iterator Object

Ah, so you're not looking to parse the iterator,  you're just looking to loop through it? Here you go:

while ($Object = $Iterator->next())
{
    print_r($Object);
}

The Iterator class is defined in QuickBooks/Iterator.php. You can look at the class to see the methods it supports.

Re: API Iterator Object

Great, that works.  Thank you for your help.  Those curious, the exact code to extract the Item list from QBWC and insert into your app:

//In api_client.php
$API->getItemByName('','_quickbooks_item_query_callback');

//In api_server.php
function _quickbooks_item_query_callback($method, $action, $ID, &$err, $qbxml, $Items, $resource){
    global $dsn;
    
    if(is_object($Items)){
        while ($Object = $Items->next()){
            $new_arr['name'] = $Object->getName();
            $new_arr['ListId'] = $Object->getListID();
            $new_arr['base_rate'] = $Object->get('SalesOrPurchase Price');
            $new_arr['invoice_item_active'] = ($Object->get('IsActive') == true) ? 1 : 0;
            //write to db  
        }
    }
        
    QuickBooks_Utilities::log($dsn, $x .' Received Item List: ' . print_r($Items, true));
}