Read XML in PHP and get your data

0

I'm doing an integration with PagSeguro, and in return it shows me an XML in this form:

<?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>2</type>  
        <code>202</code>  
    </paymentMethod>  
    <grossAmount>300021.45</grossAmount>
    <discountAmount>0.00</discountAmount>
    <creditorFees>
        <intermediationRateAmount>0.40</intermediationRateAmount>
        <intermediationFeeAmount>11970.86</intermediationFeeAmount>
    </creditorFees>
    <netAmount>288050.19</netAmount>
    <extraAmount>0.00</extraAmount>  
    <installmentCount>1</installmentCount>  
    <itemCount>3</itemCount>  
    <items>  
        <item>  
            <id>0001</id>  
            <description>Produto PagSeguroI</description>  
            <quantity>1</quantity>  
            <amount>99999.99</amount>  
        </item>  
        <item>  
            <id>0002</id>  
            <description>Produto PagSeguroII</description>  
            <quantity>2</quantity>  
            <amount>99999.98</amount>  
        </item>  
    </items>  
    <sender>  
        <name>José Comprador</name>  
        <email>[email protected]</email>  
        <phone>  
            <areaCode>99</areaCode>  
            <number>99999999</number>  
        </phone>  
    </sender>  
    <shipping>  
        <address>  
            <street>Av. PagSeguro</street>  
            <number>9999</number>  
            <complement>99o andar</complement>  
            <district>Jardim Internet</district>  
            <postalCode>99999999</postalCode>  
            <city>Cidade Exemplo</city>  
            <state>SP</state>  
            <country>ATA</country>  
        </address>  
        <type>1</type>  
        <cost>21.50</cost>  
    </shipping>
</transaction>

My PHP code to get this result and read the data looks like this:

<?php

$notificationCode = preg_replace('/[^[:alnum:]-]/','',$_POST["notificationCode"]);

        $data['token'] ='MEU TOKEN';
        $data['email'] = 'MEU E-MAIL';


        $url = 'https://ws.sandbox.pagseguro.uol.com.br/v3/transactions/notifications/'.$notificationCode.'?'.$data;

        $data = http_build_query($data);

        $curl = curl_init($url);

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_URL, $url);
        $xml = curl_exec($curl);
        curl_close($curl);

        $xml = simplexml_load_string($xml);

        $reference = $xml->reference;
        $status = $xml->status;

        if($reference && $status){
         require 'conexao.php';  

         $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


        $consulta = $pdo->prepare("SELECT * FROM compras WHERE (id = '$reference')");
        $consulta->execute();  

        $linha = $consulta->rowCount();    

//         $conn = new conecta();

//         $rs_pedido = $conn->consultarPedido($reference);

         if($linha){
            $atualiza = $pdo->prepare("UPDATE compras SET (status = '$status') where (id = '$reference')");
            $atualiza->execute();  

             echo '<script> window.location="index.php" </script>';
         }
        }



?>

However, PagSeguro returns the following error:

> POST /temp/notificacao.php HTTP/1.1
> User-Agent: Jakarta Commons-HttpClient/3.1
> Host: vovocooks.com.br
> Proxy-Connection: Keep-Alive
> Content-Length: 85
> Content-Type: application/x-www-form-urlencoded
< HTTP/1.1 200 OK
< Date: Sat, 17 Jun 2017 13:35:00 GMT
< Server: Apache
< Content-Length: 761
< Content-Type: text/html
< Via: 1.1 pagseguro.proxy.srv.intranet
< Connection: close
< <br />
< <b>Warning</b>:  simplexml_load_string() [<a href='function.simplexml-load-string'>function.simplexml-load-string</a>]: Entity: line 1: parser error : Start tag expected, '<' not found in <b>/home/storage/a/f7/10/vovocooks/public_html/temp/notificacao.php</b> on line <b>21</b><br />
< <br />
< <b>Warning</b>:  simplexml_load_string() [<a href='function.simplexml-load-string'>function.simplexml-load-string</a>]: Unauthorized in <b>/home/storage/a/f7/10/vovocooks/public_html/temp/notificacao.php</b> on line <b>21</b><br />
< <br />
< <b>Warning</b>:  simplexml_load_string() [<a href='function.simplexml-load-string'>function.simplexml-load-string</a>]: ^ in <b>/home/storage/a/f7/10/vovocooks/public_html/temp/notificacao.php</b> on line <b>21</b><br />

The line 21 reported in this error is just reading this XML:

  $xml = simplexml_load_string($xml);

What am I doing wrong?

    
asked by anonymous 17.06.2017 / 15:38

0 answers