Consume php Web Api

Asked

Viewed 365 times

1

I need to consume the following XML:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
    <NewDataSet>
        <CarregaMunicipioFranqueados>
            <Codigo_Municipio>2700201</Codigo_Municipio>
            <codigo_pais>1058</codigo_pais>
            <Pais>BRASIL</Pais>
            <sigla_uf>AL</sigla_uf>
            <Uf>ALAGOAS (AL)</Uf>
            <Municipio>ANADIA</Municipio>
            <Codigo_franqueado>490</Codigo_franqueado>
            <Franqueado>ANDERSON TESTE</Franqueado>
        </CarregaMunicipioFranqueados>
        <CarregaMunicipioFranqueados>
            <Codigo_Municipio>3100203</Codigo_Municipio>
            <codigo_pais>1058</codigo_pais>
            <Pais>BRASIL</Pais>
            <sigla_uf>MG</sigla_uf>
            <Uf>MINAS GERAIS (MG)</Uf>
            <Municipio>ABAETE</Municipio>
            <Codigo_franqueado>490</Codigo_franqueado>
            <Franqueado>ANDERSON TESTE</Franqueado>
        </CarregaMunicipioFranqueados>
    </NewDataSet>
</string>

I need to search, for example, 3100203 and the name of Municipio ABAETE

This XML is in a URL, with the GET method I will consume this data on a site in PHP.

  • In this case it seems that you need to deserialize XML, not get it from a web-api?

1 answer

1


I believe you are trying to interpret the XML you received from the web-api. In this case, you can use the SimpleXML PHP itself to interpret. Be below a simple example using SimpleXMLElement, using your own XML, and extracting the data you gave as an example:

<?php
$xml = "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">
    <NewDataSet>
        <CarregaMunicipioFranqueados>
            <Codigo_Municipio>2700201</Codigo_Municipio>
            <codigo_pais>1058</codigo_pais>
            <Pais>BRASIL</Pais>
            <sigla_uf>AL</sigla_uf>
            <Uf>ALAGOAS (AL)</Uf>
            <Municipio>ANADIA</Municipio>
            <Codigo_franqueado>490</Codigo_franqueado>
            <Franqueado>ANDERSON TESTE</Franqueado>
        </CarregaMunicipioFranqueados>
        <CarregaMunicipioFranqueados>
            <Codigo_Municipio>3100203</Codigo_Municipio>
            <codigo_pais>1058</codigo_pais>
            <Pais>BRASIL</Pais>
            <sigla_uf>MG</sigla_uf>
            <Uf>MINAS GERAIS (MG)</Uf>
            <Municipio>ABAETE</Municipio>
            <Codigo_franqueado>490</Codigo_franqueado>
            <Franqueado>ANDERSON TESTE</Franqueado>
        </CarregaMunicipioFranqueados>
    </NewDataSet>
</string>";

$simples = new SimpleXMLElement($xml);

//Pega o segundo...
$segundo = $simples->NewDataSet->CarregaMunicipioFranqueados[1];
//Pega o codigo
$codigo = $segundo->{'Codigo_Municipio'};
$municipio = $segundo->Municipio;

echo "Esse é o codigo: $codigo";
echo "<br/>";
echo "Esse é o municipio: $municipio";
?>
  • Thanks for helping... Actually this xml code is in a URL, I would have to pass a parameter to get some information, like the city by the code. Exp. <Codigo_municipio>3100203</Codigo_municipio> as parameter to print the city <Municipio>ABAETE</Municipio>

  • Got it ... in this case, who made the web-api? Every web-api is a very different thing, so you would need to know more about this specific web-api. Or is it that seeking a web-api that can do this? Or maybe you’re using one but have no documentation?

  • Then, it is in this url (http://vallesw.azurewebsites.net/api/municipiosfranqueados) Mas é provisorio apenas para testes I create a site that will fetch the city, address and tel when it is changed in the system it will change in the site automatically but I’m not able to handle this xml by url

  • If you receive all this XML from the Web API, the only way to extract the data is as I showed in the answer. If you want to receive one or two Webapi data based on some parameters, you will have to change the Webapi itself to return specific data depending on the parameters.

  • Can you use your code using a URL? instead of putting XML in the code itself?

  • Yes - instead of putting it directly into a variable, pick it up using the cURL.

Show 1 more comment

Browser other questions tagged

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