angelleye Posted June 16, 2007 Share Posted June 16, 2007 Take a look at the following XML response: http://angelleye.com/TransactionDetails.xml This data is the details of a single transaction so I don't have to worry about looping through a bunch of different nodes with the same name or anything like that. I'm simply trying to get all of the data stored into PHP variables so that I can use them later in SQL queries. I've never worked much with arrays, so the only way I know how to do this is like so... $DOM -> LoadXML($TransactionDetails); $PayerEmail = $TransactionDetailsDOM -> getElementsByTagName("Payer"); $PayerEmail = $PayerEmail -> item(0) -> nodeValue; $PayerID = $TransactionDetailsDOM -> getElementsByTagName("PayerID"); $PayerID = $PayerID -> item(0) -> nodeValue; $PayerStatus = $TransactionDetailsDOM -> getElementsByTagName("PayerStatus"); $PayerStatus = $PayerStatus -> item(0) -> nodeValue; etc. etc. etc. That ends up taking a bunch of lines of code and I know there's a better way. Is there a way that I can create an array to store all element names/values? For instance, $data = array("Payer"=>andrew@angelleye.com, "PayerID"=>XZE6NYGHU7E7W, "PayerStatus"=>unverified, etc. etc. etc); And then, once the data is in the array I know I have to access it like $data(0), $data(1), etc. But is there a way I can call those by the name of the paramater instead of having to know the number to use? So within my SQL I could use $data(ParamaterName) where I need that data to be inserted...??? Any information on this would be greatly appreciated. Thanks!!!! Quote Link to comment https://forums.phpfreaks.com/topic/55797-how-to-get-xml-data-into-php-array-and-then-access-data-by-element-names/ Share on other sites More sharing options...
emehrkay Posted June 16, 2007 Share Posted June 16, 2007 i think you're going about it the right way. i would say that it would be pretty difficult to just loop through the xml grabbing the node names and values and just poppin that into an array because of the node names nto being unique (there are a few "names" for examples) i may be wrong though, good luck Quote Link to comment https://forums.phpfreaks.com/topic/55797-how-to-get-xml-data-into-php-array-and-then-access-data-by-element-names/#findComment-275652 Share on other sites More sharing options...
angelleye Posted June 16, 2007 Author Share Posted June 16, 2007 If I had to I could do it in chunks. One array for all the data in the PayerName node. Another array for the data in the Address node. Another for the PaymentInfo node. etc. I guess what I need is a function that handles generating an array out of a given xml string...?? I've been looking around and I don't see anything like that anywhere but I feel like I must not be looking in the right place. Seems like something somebody would have done before. Quote Link to comment https://forums.phpfreaks.com/topic/55797-how-to-get-xml-data-into-php-array-and-then-access-data-by-element-names/#findComment-275704 Share on other sites More sharing options...
angelleye Posted June 17, 2007 Author Share Posted June 17, 2007 I came across a class that converts the XML to an associative array rather nicely: http://eric.pollmann.net/work/public_domain/. So now you can see the output of the array here: http://angelleye.com/temp/XMLParserTest.php. Now I just need to figure out how to more easily grab data out of this array tree..?? For example, in order to grab the value of the <Ack> element in the XML in the converted array I have the following: $RequesterCredentials = $XMLTree['SOAP-ENV:ENVELOPE']; $SoapBody = $RequesterCredentials['SOAP-ENV:BODY']; $GetTransactionDetailsResponse = $SoapBody['GETTRANSACTIONDETAILSRESPONSE']; $Ack = $GetTransactionDetailsResponse['ACK']; $AckValue = $Ack['VALUE']; echo $AckValue; Seems like a lot of work to get there, but that's the only way I could figure it out with the nested arrays. Any links to guides on parsing array trees like this would be cool. I can't find anything but basic information about associative arrays. For example, is there a way to load the $AckValue variable in a single line without having to go through everything else first? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/55797-how-to-get-xml-data-into-php-array-and-then-access-data-by-element-names/#findComment-276113 Share on other sites More sharing options...
angelleye Posted June 17, 2007 Author Share Posted June 17, 2007 Ok, I've been making good progress tonight but now I'm stuck again. I've got my transaction details in an array called $TransactionDetailsArray. When I print_r I get the following: Array ( [PayerEmailAddress] => blah@hey.com [PayerID] => LFPUDEQ85LG86 [PayerStatus] => unverified [PayerSalutationName] => [PayerFirstName] => John [PayerMiddleName] => [PayerLastName] => Holmes [PayerSuffixName] => [PayerCountryCode] => US [PayerBusiness] => [shippingName] => John Holmes [shippingAddress] => 3234 E. Westwood Ave. [shippingAddress2] => [shippingCity] => Grandview [shippingStateOrProvince] => MO [shippingCountryCode] => US [shippingCountryName] => United States [shippingPostalCode] => 64030 [shippingAddressOwner] => PayPal [shippingAddressStatus] => Unconfirmed [TransactionID] => 1RU522029N447015J [ReceiptID] => 0546-8398-3858-9564 [TransactionType] => cart [PaymentType] => instant [PaymentDate] => 2007-06-17T05:25:44Z [GrossAmountPaid] => 862.16 [PayPalFee] => 25.30 [TotalSalesTax] => 0.00 [ExchangeRate] => [PaymentStatus] => Completed [PaymentPendingReason] => none [PaymentReasonCode] => none [invoiceID] => [CustomValues] => 816-977-5402 | UPS Ground [Memo] => [PaymentItemSalesTax] => 0.00 [invoiceItems] => Array ( [0] => Array ( [ATTRIBUTES] => Array ( [xmlNS] => urn:ebay:apis:eBLBaseComponents [XSI:TYPE] => ebl:PaymentItemType ) [NAME] => Array ( [ATTRIBUTES] => Array ( [XSI:TYPE] => xs:string ) [VALUE] => Microsoft Windows Server 2003 Standard w/ 5 CAL OEM ) [NUMBER] => Array ( [ATTRIBUTES] => Array ( [XSI:TYPE] => xs:string ) [VALUE] => 26611 ) [QUANTITY] => Array ( [ATTRIBUTES] => Array ( [XSI:TYPE] => xs:string ) [VALUE] => 1 ) [sALESTAX] => Array ( [ATTRIBUTES] => Array ( [XSI:TYPE] => xs:string ) [VALUE] => ) ) [1] => Array ( [ATTRIBUTES] => Array ( [xmlNS] => urn:ebay:apis:eBLBaseComponents [XSI:TYPE] => ebl:PaymentItemType ) [NAME] => Array ( [ATTRIBUTES] => Array ( [XSI:TYPE] => xs:string ) [VALUE] => Enterasys 1 Port Channelized ISDN CSU/DSU Modem ) [NUMBER] => Array ( [ATTRIBUTES] => Array ( [XSI:TYPE] => xs:string ) [VALUE] => 26619 ) [QUANTITY] => Array ( [ATTRIBUTES] => Array ( [XSI:TYPE] => xs:string ) [VALUE] => 1 ) [sALESTAX] => Array ( [ATTRIBUTES] => Array ( [XSI:TYPE] => xs:string ) [VALUE] => ) ) ) [AuctionEbayID] => ) I'm trying to loop through the embedded InvoiceItems array and I'm having trouble figuring it out. For some reason this array isn't working the same for me as all the rest I've bee playing with..?? Here's what I've been trying: foreach($TransactionDetailsArray['InvoiceItems'] as $InvoiceItem) { $ItemName = $InvoiceItem['NAME']['VALUE']; $ItemNumber = $InvoiceItem['NUMBER']['VALUE']; $ItemQTY = $InvoiceItem['QUANTITY']['VALUE']; } When I run my page I'm getting a blank white page with no errors displayed even though I've included ini_set('display_errors', 1); error_reporting(E_ALL); at the top of my script. So now I'm stuck becuase that's how I've been looping through my other arrays so I'm not sure why this one isn't the same..?? Any help would be great. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/55797-how-to-get-xml-data-into-php-array-and-then-access-data-by-element-names/#findComment-276216 Share on other sites More sharing options...
demon_athens Posted June 17, 2007 Share Posted June 17, 2007 have you seen this? http://www.phpfreaks.com/script/view/202.php Quote Link to comment https://forums.phpfreaks.com/topic/55797-how-to-get-xml-data-into-php-array-and-then-access-data-by-element-names/#findComment-276222 Share on other sites More sharing options...
angelleye Posted June 18, 2007 Author Share Posted June 18, 2007 Actually I did find that, and now I'm running into another problem with all of this. Using the XML Parser I've managed to create a function that will return all of the transaction details of a PayPal payment to my script in an associative array. I posted a sample of that data previously when I was having trouble traversing it. I've figured that out, but now this other problem is hitting me. The items on the invoice come back in an embedded array called InvoiceItems. This particular example has 2 items on the order, therefore, in order to grab the values from it I had to use: foreach($TransactionDetailsArray['InvoiceItems'] as $key => $ItemDataArray) { $ItemNumber = $ItemDataArray['NUMBER']['VALUE']; $ItemQTY = $ItemDataArray['QUANTITY']['VALUE']; $SQL = "INSERT INTO Invoice_Items (Invoice_Number, Item_Number, Quantity) VALUES (" . $InvoiceNumber . ", " . $ItemNumber . ", " . $ItemQTY . ")"; echo $SQL . "<br /><br />"; odbc_exec($FMInvoiceItemsConnection,$SQL); } //end for each to insert invoice items. However, many orders only come through with a single item on them. In these cases the InvoiceItems array simply contains the data for that one item and I don't need $key -> $value in the foreach() statement. In fact, I can't use it or I get errors and I don't get what I want back. So then I was trying to think of how to fix this little problem and I tried using count($TransactionDetails['InvoiceItems']) but that doesn't work out because if only 1 item is on the order then that comes back as 5 because there are 5 peices of data in that array. So that's where I'm stuck now. Any more info would be great. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/55797-how-to-get-xml-data-into-php-array-and-then-access-data-by-element-names/#findComment-276617 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.