You are here:  » getting data back out of function


getting data back out of function

Submitted by msimonds on Mon, 2008-08-11 17:00 in

Sir,

This is my XML data:

{code saved}

I am trying to get the data in the array out of the function, but it returns a boolean of true or false:

I need to be able to get the data back out of the function and process it not inside the function

<?php
require ("MagicParser.php");
//$dwin_data = array();
function myRecordHandler($record)
{
    require_once (
'/users/msimonds/public_html/includes/sandbox_login.inc');
    
$dwin_data['Allocation__c'] = $record['SOBJECT/SF:ALLOCATION__C'];
    
$dwin_data['Competitors__c'] = $record['SOBJECT/SF:COMPETITORS__C'];
    
$dwin_data['Dedicated_FAE__c'] = $record['SOBJECT/SF:DEDICATED_FAE__C'];
    
$dwin_data['Description_c__c'] = $record['SOBJECT/SF:DESCRIPTION_C__C'];
    
$dwin_data['Oli_Id'] = $record['SOBJECT/SF:ID'];
    
$dwin_data['OpportunityId'] = $record['SOBJECT/SF:OPPORTUNITYID'];
    
$dwin_data['Part_Outcome__c'] = $record['SOBJECT/SF:PART_OUTCOME__C'];
    
$dwin_data['Potential__c'] = $record['SOBJECT/SF:POTENTIAL__C'];
    
$dwin_data['Price_Type__c'] = $record['SOBJECT/SF:PRICE_TYPE__C'];
    
$dwin_data['PricebookEntryId'] = $record['SOBJECT/SF:PRICEBOOKENTRYID'];
    
$dwin_data['Quantity'] = $record['SOBJECT/SF:QUANTITY'];
    
$dwin_data['Quote_No__c'] = $record['SOBJECT/SF:QUANTITY'];
    
$dwin_data['Socket_Label__c'] = $record['SOBJECT/SF:SOCKET_LABEL__C'];
    
$dwin_data['UnitPrice'] = $record['SOBJECT/SF:UNITPRICE'];
    echo 
'<pre>'.print_r($dwin_data,true).'</pre>';
    return 
$dwin_data;
    
//return $dwin_data;
}
$data fopen('test.xml','r');
//$data = fopen('php://input','rb');
$content stream_get_contents($data);
$process MagicParser_parse("string://".$content,"myRecordHandler","xml|SOAPENV:ENVELOPE/SOAPENV:BODY/NOTIFICATIONS/NOTIFICATION/");
echo 
'<pre>'.print_r($process,true).'</pre>';
?>

Can you help me understand why this is not happening?

Submitted by support on Mon, 2008-08-11 17:09

Hello,

The return value from myRecordHandler can never be used by your application - its return
value is only used to tell Magic Parser whether or not to continue reading records.

Instead, what you need to so is simply to make sure that the variable you are loading
the values into is "global", and then you can use the data in your application as
normal!

However, this is complicated by the fact that you have multiple notifications in your
XML; so if you want to process them outside of myRecordHandler, you have to add them
all to an array, and then loop through the array to process each one.

The code below will load each notification into an array, and then loop through each
item and print_r() foreach notification; hopefully this will let you do what you want.

I have also move the require() outside of myRecordHandler as this is the best place
for require() statements:

<?php
require ("MagicParser.php");
require_once (
'/users/msimonds/public_html/includes/sandbox_login.inc');
//$dwin_data = array();
$notifications = array();
function 
myRecordHandler($record)
{
    global 
$notifications;
    
$dwin_data['Allocation__c'] = $record['SOBJECT/SF:ALLOCATION__C'];
    
$dwin_data['Competitors__c'] = $record['SOBJECT/SF:COMPETITORS__C'];
    
$dwin_data['Dedicated_FAE__c'] = $record['SOBJECT/SF:DEDICATED_FAE__C'];
    
$dwin_data['Description_c__c'] = $record['SOBJECT/SF:DESCRIPTION_C__C'];
    
$dwin_data['Oli_Id'] = $record['SOBJECT/SF:ID'];
    
$dwin_data['OpportunityId'] = $record['SOBJECT/SF:OPPORTUNITYID'];
    
$dwin_data['Part_Outcome__c'] = $record['SOBJECT/SF:PART_OUTCOME__C'];
    
$dwin_data['Potential__c'] = $record['SOBJECT/SF:POTENTIAL__C'];
    
$dwin_data['Price_Type__c'] = $record['SOBJECT/SF:PRICE_TYPE__C'];
    
$dwin_data['PricebookEntryId'] = $record['SOBJECT/SF:PRICEBOOKENTRYID'];
    
$dwin_data['Quantity'] = $record['SOBJECT/SF:QUANTITY'];
    
$dwin_data['Quote_No__c'] = $record['SOBJECT/SF:QUANTITY'];
    
$dwin_data['Socket_Label__c'] = $record['SOBJECT/SF:SOCKET_LABEL__C'];
    
$dwin_data['UnitPrice'] = $record['SOBJECT/SF:UNITPRICE'];
    
$notifications[] = $dwin_data;
}
$data fopen('test.xml','r');
//$data = fopen('php://input','rb');
$content stream_get_contents($data);
$process MagicParser_parse("string://".$content,"myRecordHandler","xml|SOAPENV:ENVELOPE/SOAPENV:BODY/NOTIFICATIONS/NOTIFICATION/");
foreach(
$notifications as $notification)
{
  echo 
'<pre>'.print_r($notification,true).'</pre>';
}
?>

This will work fine, but only with a small number of notifications. The above code has
to load all notifications into memory, so if your XML file is likely to be very large
this will not work and you will run out of memory - this is why it is best to process
each record within myRecordHandler().

Hope this helps!
Cheers,
David.

Submitted by msimonds on Mon, 2008-08-11 17:30

Thanks David for the reply, I appreciate it

I figured it was a stupid mistake on my part and forgot to add [] to my array inside the function

I tried to process it inside the function:

{code saved}

but it is only processing one record.

If you can see where I am going wrong, i would be TOTALLY grateful

~Mike

Submitted by support on Mon, 2008-08-11 17:44

Hello Mike,

I notice that you have:

    exit();

...on line 60 within your myRecordHandler() function; which would certainly cause it to stop after the first record - so that may be all it is...

Cheers,
David.