How to write specific XML tags in PHP to MySQL

0

I'm reading a XML in PHP to write to MySQL , the XML is PagSeguro , I'm already saving some data as the head of the request, but the client wants the purchase information be available in the buyer area on the site even though he knows that whoever made the purchase has it in the PagSeguro area. So I've already read XML and I have information available, how can I now write to a table?

XML has this structure:

Again I put as HTML snippet because I could not insert it as a code sample, it was all messed up.

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<transaction>
	<date>2011-02-10T16:13:41.000-03:00</date>
	<code>9E884542-81B3-4419-9A75-BCC6FB495EF1</code>
	<reference>REF1234</reference>
	<type>1</type>
	<status>3</status>
	<paymentMethod>
		<type>1</type>
		<code>101</code>
	</paymentMethod>
	<grossAmount>49900.00</grossAmount>
	<discountAmount>0.00</discountAmount>
	<feeAmount>0.00</feeAmount>
	<netAmount>49900.00</netAmount>
	<extraAmount>0.00</extraAmount>
	<installmentCount>1</installmentCount>
	<itemCount>2</itemCount>
	<items>
		<item>
			<id>0001</id>
			<description>Notebook Prata</description>
			<quantity>1</quantity>
			<amount>24300.00</amount>
		</item>
		<item>
			<id>0002</id>
			<description>Notebook Rosa</description>
			<quantity>1</quantity>
			<amount>25600.00</amount>
		</item>
	</items>
	<sender>
		<name>José Comprador</name>
		<email>[email protected]</email>
		<phone>
			<areaCode>11</areaCode>
			<number>56273440</number>
		</phone>
	</sender>
	<shipping>
		<address>
			<street>Av. Brig. Faria Lima</street>
			<number>1384</number>
			<complement>5o andar</complement>
			<district>Jardim Paulistano</district>
			<postalCode>01452002</postalCode>
			<city>Sao Paulo</city>
			<state>SP</state>
			<country>BRA</country>
		</address>
		<type>1</type>
		<cost>21.50</cost>
	</shipping>
</transaction>

What I have already recovered from XML

// DADOS DO XML
$IDProdutos = $transaction->items->item->id; 
$ItensPedido = $transaction->itemCount;        
$Parcelas = $transaction->installmentCount;    
$TaxaParcelas = $transaction->installmentFeeAmount;  
$Status = $transaction->status;     
$Desconto =  $transaction->extraAmount; 
$TipoFrete = $transaction->shipping->type; 
$ValorFrete = $transaction->shipping->cost;     
$ValorProduto = $transaction->items->item->amount; 
$QuantidadeProduto = $transaction->items->item->quantity; 
$DescricaoProduto = $transaction->items->item->description; 
// CONTATO DO COMPRADOR 
$NomeComprador = $transaction->sender->name;    
$EmailComprador = $transaction->sender->email;
$DDDComprador = $transaction->sender->phone->areaCode;
$TelefoneComprador = $transaction->sender->phone->number;
// ENDEREÇO DO COMPRADOR    
$EnderecoComprador = $transaction->shipping->address->street;
$NumeroComprador = $transaction->shipping->address->number;
$ComplementoComprador = $transaction->shipping->address->complement;
$DistritoComprador = $transaction->shipping->address->district;
$CEPComprador = $transaction->shipping->address->postalCode;
$CidadeComprador = $transaction->shipping->address->city;
$UFComprador =  $transaction->shipping->address->state;

Again I put HTML as an excerpt because I could not insert it as a code sample, it was all messed up. What I need is to write the ItemCount, OrderItems, TransactionId, and BuyerID I'm trying like this:

if ($retorno) {

    // PEDIDOS
    foreach($transaction->itens->item as $item) {

        $sql = 'INSERT INTO pagseguropedidos (TransacaoID,CompradorID,IdProduto,DescricaoProduto,QuantidadeProduto,ValorProduto) VALUES (?,?,?,?,?,?)';
        $stm = $conexao->prepare($sql);
        $stm->bindValue(1, $TransacaoID, PDO::PARAM_INT);
        $stm->bindValue(2, $CompradorID, PDO::PARAM_INT);
        $stm->bindValue(3, $IDProdutos, PDO::PARAM_STR);
        $stm->bindValue(3, $DescricaoProduto, PDO::PARAM_STR);
        $stm->bindValue(4, $QuantidadeProduto, PDO::PARAM_INT);
        $stm->bindValue(5, $ValorProduto, PDO::PARAM_INT);
        $stm->execute();

    }
}

Of course, Foreach will not work that way and that's where the question comes up, how to write those records in my table?

    
asked by anonymous 24.10.2018 / 22:38

0 answers