Parser error : Start tag expected, '<' not found

Asked

Viewed 4,700 times

3

I can understand the script, except for the part that he works on XML... so I can’t tell if the mistake is in the script, or in the XML that he tries to get.

Code:

<?php
 require ("includes/connection.php");
 require ("includes/start-session.php");
 require ("includes/encript.php");
?>
<?php
header("access-control-allow-origin: https://ws.sandbox.pagseguro.uol.com.br");

$email = 'email@sandboxpagseguro';
$token = 'tokensandbox';

$pagamento = $_GET['transaction_id'];
$url = 'https://ws.sandbox.pagseguro.uol.com.br/v2/transactions/'. $pagamento .'?email=' .$email. '&token=' .$token;

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$transaction= curl_exec($curl);
curl_close($curl);

if($transaction == 'Unauthorized'){
    //Caso o token ou e-mail não sejam validados pelo PagSeguro.
    echo 'Unauthorized'; 
    exit;
}
$transaction = simplexml_load_string($transaction);
if($transaction->code > 0) {
$transaction_id = $transaction->code;
$client_id = $transaction->reference;        
    $payment_type = $transaction->paymentMethod->type;
    if($payment_type == 1){ 
        $payment_method = "Cartão de crédito";
    } elseif($payment_type == 2){ 
        $payment_method = "Boleto";
    } elseif($payment_type == 3){ 
        $payment_method = "Débito online (TEF)"; 
    } else { 
        $payment_method = "Outro"; 
    }
    $payment_type_method = $transaction->type;
        if($payment_type_method == 1){ 
        $payment_method_transiction = "Pagamento";
    } elseif($payment_type_method == 11){ 
        $payment_method_transiction = "Assinatura";
    } else { 
        $payment_method_transiction = "Outro"; 
    }

    $parceled = $transaction->installmentCount;
    $parceled_value = $transaction->installmentFeeAmount;
    $product = $transaction->items->item->id;
    $product_value = $transaction->items->item->amount;
    $transaction_date = date('d/m/Y', strtotime($transaction->date));
    $transaction_date_last = date('d/m/Y', strtotime($transaction->lastEventDate));
    if($transaction->status == 1){
        $transaction_status = 'Aguardando pagamento';
    } elseif($transaction->status == 2){
        $transaction_status = 'Em análise';
    } elseif($transaction->status == 3){ // :)
        $transaction_status = 'Paga';
    } elseif($transaction->status == 4){ // :D
        $transaction_status = 'Disponível';
    } elseif($transaction->status == 5){
        $transaction_status = 'Em disputa';
    } elseif($transaction->status == 6){
        $transaction_status = 'Devolvida';
    } elseif($transaction->status == 7){
        $transaction_status = 'Cancelada';
    }   
    $client_name = $transaction->sender->name;

    // Faz a inserção no BD.
    $insert = $mysqli->query("INSERT INTO `payments`(`transaction_id`, `client_id`, `payment_method`, `payment_method_transiction`, `transaction_status`, `transaction_date`, `transaction_date_last`, `product`, `product_value`, `client_name`) VALUES ('$transaction_id', '$client_id', '$payment_method', '$payment_method_transiction', '$transaction_status', '$transaction_date', '$transaction_date_last', '$product', '$product_value', '$client_name')");
    if ($insert) {
        echo 'dados inseridos';
    } else {
        echo 'falha na inserção dos dados.';
    }
} else {
    echo $transaction->code;
}
?>

Parse Error:

Warning: simplexml_load_string(): Entity: line 1: parser error : Start Tag expected, '<' not found in /home/u657579475/public_html/loja/confirm.php on line 26

Warning: simplexml_load_string(): Forbidden in /home/u657579475/public_html/loja/confirm.php on line 26

Warning: simplexml_load_string(): in /home/u657579475/public_html/loja/confirm.php on line 26

Line 26:

$transaction = simplexml_load_string($transaction);
  • 1

    Take a look at: https://github.com/gabrielsbarros/pagseguro/blob/master/PagSeguro.php. You’ll need to use libxml_use_internal_errors(true) to suppress possible XML errors.

  • Just call the php script via require? and insert this code before converting the xml to object?

  • 1

    Yes. Actually the ideal is to use a autoload, but works perfectly with require.

1 answer

3


This error message is typically caused by bad XML formatting.

Check whether the contents of $transaction contains a valid XML string.

To test, in this section

$transaction = simplexml_load_string($transaction);

Trade for this:

echo $transaction; exit;
$transaction = simplexml_load_string($transaction);

This is not the solution to the problem. It’s just a debugging technique to find the causes of the problem more easily.

Run and see the result in the generated HTML code. Press CTRL+U if you are using Google Chrome.

The intention is to read the contents of the variable $transaction.

If you don’t know how to interpret XML visually, post the test result in your question.

  • Okay, I’ll do it.

  • Daniel, the result was: Forbidden.

  • 1

    this is the problem... fix the connection to the api

Browser other questions tagged

You are not signed in. Login or sign up in order to post.