Simplexml does not return the file data

Asked

Viewed 210 times

1

I’m using of to retrieve data from a file , but nothing returns.

What can it be ?

<?php

@header('Content-Type: text/html; charset=utf-8');

$xml = simplexml_load_file("http://www.multicinecinemas.com.br/webservice/?chave=c99e34ed77750774417e5a9d2a2f5135&tipo=cinema");

foreach($xml->filme as $filme)
{
 echo $filme->codigo;
 echo $filme->nome;
 echo $filme->horario;
 echo "<br>";
}
?>
  • The form q vc is iterating the xml that is wrong. Note that in the structure of the file vc does not start with film, it starts with cinemas > cinema > movies

  • How do I make it work? I don’t know about these "sub-levels"

1 answer

2


To get to print out only the film need to navigate to the correct point:

$xml->CINEMAS->CINEMA->FILMES->FILME

For example:

<?php

    $url = 'http://www.multicinecinemas.com.br/webservice/'
    $url .= '?chave=c99e34ed77750774417e5a9d2a2f5135&tipo=cinema';

    $xml = simplexml_load_file($url);

    foreach($xml->CINEMAS->CINEMA->FILMES->FILME as $filme)
    {       
        echo $filme->CODIGO;
        echo ' ';
        echo $filme->NOME;
        echo ' ';
        echo $filme->HORARIO;
        echo ' ';
        echo "<br>";
    }

To repeat the same process for several files use a function as is:

function toPrint(array $array = array())
{
    foreach($array as $items)
    {
        foreach($items->CINEMAS->CINEMA->FILMES->FILME as $filme)
        {           
            echo $filme->CODIGO;
            echo ' ';
            echo $filme->NOME;
            echo ' ';
            echo $filme->HORARIO;
            echo ' ';
            echo "<br>";
        }
    }
}

$url = 'http://www.multicinecinemas.com.br/webservice/'
$url .= '?chave=c99e34ed77750774417e5a9d2a2f5135&tipo=cinema';
$xml = simplexml_load_file($url);

toPrint(array($xml1, $xml2));
  • It worked. In this case, I need to print out data from 2 xml files. How would I insert into a $xml2 variegable the data of the other file, within the foreach? Type: foreach($xml->CINEMAS->CINEMA->MOVIES->FILM as $film, $xml2->MOVIES->FILM as $filme2)

  • @Leoamaral repeat the same procedure for the xml2, can build a function to print and pass the two files, I will edit and propose a function.

Browser other questions tagged

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