XML returning empty in PHP

0

Good afternoon,

I am running queries 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 return if I make the request in POSTMAN it gives me this return:

ButdoingthePHPrequestreturnsmeemptywithvar_dump()likethis:

object(SimpleXMLElement)#40(0){}

code:

functionconsultaScr($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 get the $ xml variable and write it to a txt file it has the same content as POSTMAN, but even if I generate an .xml file with the fopen () function and after reading this same, it returns empty!

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

I do not know what I'm doing wrong. Thanks in advance for your help.

FULL XML READING AND RECORDING PROCESS

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);
}
    
asked by anonymous 27.12.2016 / 17:28

2 answers

0

View soap functions or invoke the 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 literally show. The browser hides the tags. The literal result of the browser can be seen in the source code. In chrome, press CTRL + U and F5 to ensure the updated data.

    
27.12.2016 / 18:21
0

I tried to do this as explained above, but did not resolve either:

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'));
}

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

    
27.12.2016 / 18:36