简体   繁体   中英

How can i retrieve specific xml data from this URL?

I need to retrieve the Identificador values from this URL

I've tried several approaches, but unsuccessful so far.

Here is the code:

function readxml($courseid,$datayear,$dataperiod,$datasemester){
    if ($dataperiod == 'a')
        $datasemester='1';

    $xmlUrl = 'https://clip.unl.pt/sprs?lg=pt&year=' . $datayear  .
        '&uo=97747&srv=rsu&p=' . $datasemester .
        '&tp=' . $dataperiod .
        '&md=3&rs=' . $courseid .
        '&it=1030123459';

    if (!function_exists('download_file_content')){
        function download_file_content($xmlStr){
            return implode( "",file($xmlStr));
        }
    }
    $xmlStr=download_file_content($xmlUrl);

    $xmlObj=new SimpleXMLElement(file_get_contents($xmlStr));

    foreach ($xmlObj->unidade_curricular->inscritos->aluno as $aluno) {
        $result=($aluno->identificador)."<br/>";
        echo $result;
    }
}

The download_file_content wouldn't be recognized if it wasn't inside the if statement. Currently, the error that I get is this:

Warning: file_get_contents(<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <edicao_lectiva nome="Algoritmos e Estruturas de Dados"> <ano>2008</ano> <lingua>pt</lingua> <tipo_de_periodo_lectivo>s</tipo_de_periodo_lectivo> <periodo_lectivo>1</periodo_lectivo> <unidade_curricular nome="Algoritmos e Estruturas de Dados"> <codigo>8145</codigo> <unidade_organica>Faculdade de Ciências e Tecnologia</unidade_organica> <departamento>Departamento de Informática</departamento> <grupodisciplinas>Ciência e Tecnologia da Programação</grupodisciplinas> <inscritos> <aluno numero="24250"> <nome>Adriano Constantino de Sousa Strumbudakis</nome> <identificador>a.strumbudakis</identificador> <codigo& in C:\moodle25\server\moodle\local\ecoclipaluno\somefile.php on line 106


Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in C:\moodle25\server\moodle\local\ecoclipaluno\somefile.php:106 Stack trace: #0 C:\moodle25\server\moodle\local\ecoclipaluno\somefile.php(106): SimpleXMLElement->__construct('') #1 C:\moodle25\server\moodle\local\ecoclipaluno\somefile.php(137): readxml('8145', '2008', 's', '1') #2 C:\moodle25\server\moodle\local\ecoclipaluno\somefile.php(145): xpto('8145', '2008', 's', '1') #3 {main} thrown in C:\moodle25\server\moodle\local\ecoclipaluno\somefile.php on line 106

What am I doing wrong?

I noticed now that my post was missing some information because i use tags to say what values i wanted to extract.

Like this:

<pre>
<?php

$xml = simplexml_load_string(file_get_contents('https://clip.unl.pt/sprs?lg=pt&year=2008&uo=97747&srv=rsu&p=1&tp=s&md=3&rs=8145&it=1030123459'));

print_r($xml);

?>
</pre>

Outputs:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [nome] => Algoritmos e Estruturas de Dados
        )

    [ano] => 2008
    [lingua] => pt
    [tipo_de_periodo_lectivo] => s
    [periodo_lectivo] => 1

    ... snipped

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM