XML returning empty in PHP

Asked

Viewed 542 times

0

Good afternoon,

I’m doing consultations on a central bank webservice and am encountering some difficulties. The return of the query is in XML and I must send the authentication to receive this data, so far so good. The problem is in the return if I make the request in POSTMAN he gives me this return: inserir a descrição da imagem aqui

But making the request by PHP it returns me empty with var_dump() like this:

object(SimpleXMLElement)#40 (0) { }

code:

function consultaScr($cliente, $dataBase)
{
    $url = "www3.bcb.gov.br/wsscr2/services/Scr2WebService/getResumoDoCliente?dataBase=$dataBase&codCliente=$cliente&tpCliente=1&autorizacao=S";

    $username = getenv('USERNAME_SCR');
    $password = getenv('PASSWORD_SCR');
    $xml = file_get_contents("https://$username:$password@$url");

    var_dump($xml);
}

If I take the $xml variable and write it in a txt file it has the same content as POSTMAN, but even if I generate a file. xml with the fopen() function and after reading it, returns empty!

Another detail if I give an echo $xml, it returns the content but in string format without the tags.

I don’t know what I’m doing wrong. I appreciate the help.

FULL PROCESS OF READING AND WRITING IN XML

function consultaScr($cliente, $dataBase)
{
    $url = "www3.bcb.gov.br/wsscr2/services/Scr2WebService/getResumoDoCliente?dataBase=$dataBase&codCliente=$cliente&tpCliente=1&autorizacao=S";

    $username = getenv('USERNAME_SCR');
    $password = getenv('PASSWORD_SCR');
    $xml = file_get_contents("https://$username:$password@$url");

    $fp = fopen('consulta.xml', 'w+');
    fwrite($fp, $xml);
    fclose($fp);

    $arquivo_xml = simplexml_load_file('consulta.xml');

    var_dump($arquivo_xml);
}
  • I put the full code there.

2 answers

0

See own functions of Soap or invoke the method simplexml xpath:

To use the xpath method, a simple example:

$str = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Body>
    <aaa>foo</aaa>
    <bbb>bar</bbb>
</soap:Body>
</soap:Envelope>';

// Teste com gambiarra, removendo a string "soap". (Não recomendado)
//print_r(simplexml_load_string(str_ireplace('soap:', '', $s)));

$xml = simplexml_load_string($str);

// retorna um objeto do conteúdo da soap:Body
print_r($xml->xpath('//soap:Body'));

returns

Array
(
    [0] => SimpleXMLElement Object
        (
            [aaa] => foo
            [bbb] => bar
        )

)

Obs: If you are testing in a browser, be aware that the result by the browser does not display literally. The browser hides tags. The literal result by the browser can be seen in the source code. On Chrome, press CTRL+U and F5 to ensure the updated data.

0

I tried to do this as explained above, but also did not solve:

function consultaScr($cliente, $dataBase)
{
    $url = "www3.bcb.gov.br/wsscr2/services/Scr2WebService/getResumoDoCliente?dataBase=$dataBase&codCliente=$cliente&tpCliente=1&autorizacao=S";

    $username = getenv('USERNAME_SCR');
    $password = getenv('PASSWORD_SCR');
    $xml = file_get_contents("https://$username:$password@$url");

    $novo = simplexml_load_string($xml);

    print_r($novo->xpath('//soap:Body'));
}

Considering that my return is exactly what I have in the image of POSTMAN.

Browser other questions tagged

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