Then Friend, in this first line we already have an error, you are calling the variable $ xml that does not yet exist.
<?php
$simples = new SimpleXMLElement($xml);
$xml = simplexml_load_file('http://vallesw.azurewebsites.net/api/municipiosfranqueados');
On the creation of the xml document's simpleXMLElement object, when you use simplexml_load_file()
it reads the XML document and returns an object of type SimplexmlElement (Note: if the XML has been malformed it returns false), then you need to instantiate a SimpleXMLElement, as it was done in the first line, it would just do:
<?php
$xml = simplexml_load_file('http://vallesw.azurewebsites.net/api/municipiosfranqueados');
$segundo = $xml->NewDataSet->CarregaMunicipioFranqueados[1];
$codigo = $segundo->Codigo_Municipio;
$municipio = $segundo->Municipio;
echo "Esse é o codigo: $codigo";
echo "<br/>";
echo "Esse é o municipio: $municipio";
?>
But it has a detail, I did a test here and I noticed another problem, there is a content-type problem in the page header that displays xml, and when simplexml_load_file($url)
loaded xml, it gave an error: Start tag expected, '<'
, then I made the following correction:
<?php
$url = "http://vallesw.azurewebsites.net/api/municipiosfranqueados";
$xml = file_get_contents($url); // Pega o conteúdo da url e armazena em string
$xml = stripcslashes($xml); // remove '\n','\r' da string
$xml = str_replace("\"", "",$xml); // remover aspas dulas do inicio e fim da string.
$xml = simplexml_load_string($xml); // ler a string e retorna um objeto SimpleXMLElement
$segundo = $xml->CarregaMunicipioFranqueados[1];
$codigo = $segundo->Codigo_Municipio;
$municipio= $segundo->Municipio;
echo "Esse é o codigo: $codigo";
echo "<br/>";
echo "Esse é o municipio: $municipio";
?>
I hope I have been able to help man.