Mr.X
Mr.X

Reputation: 107

PHP Output SimpleXMLElement

I‘m trying to read a SimpleXMLElement in my .php in order to complement a search function. I‘m getting the XML from a german synonym API, called openthesaurus.de, via a file_get_contents request. Then I turn the recieved element into a SimpleXMLElement and try to read the attributes of each term. (I know that the following code only focuses on the synset['0']). This was done to keep it simple.
My .php looks like this

            $url = "https://www.openthesaurus.de/synonyme/search?q=". $keyword. "&format=text/xml";
            $options = array('http' =>
                array(
                    'method'  => 'POST',
                    'header'  => 'user_agent: my-site.xyz',
                )
            );
            $context = stream_context_create($options);
            $recive = file_get_contents($url, false, $context);
            if (($recieve = file_get_contents($url))===false){
                echo "Error fetching XML\n";
            } else {
                libxml_use_internal_errors(true);
                $syn = new SimpleXMLElement($recieve);
                if (!$syn) {
                    echo "Error loading XML\n";
                    foreach(libxml_get_errors() as $error) {
                        echo "\t", $error->message;
                    }
                } else {
                    echo "content: <br>";
                    foreach($syn->synset[0]->term as $value){
                        echo $value. "<br>";
                    }
                }
            }

The xml looks like this:

<matches>
    <metaData>
        <apiVersion content="0.1.3"/>
        <warning content="WARNING -- this XML format may be extended without warning"/>
        <copyright content="Copyright (C) 2019 Daniel Naber (www.danielnaber.de)"/>
        <license content="Creative Commons Attribution-ShareAlike 4.0 or GNU LESSER GENERAL PUBLIC LICENSE Version 2.1"/>
        <source content="https://www.openthesaurus.de"/>
        <date content="Sat Jun 27 09:48:33 CEST 2020"/>
    </metaData>
    <synset id="292">
        <categories/>
        <term term="Erprobung"/>
        <term term="Probe"/>
        <term term="Prüfung"/>
        <term term="Test"/>
        <term term="Versuch"/>
    </synset>
    <synset id="4398">
        <categories/>
        <term term="Leistungsnachweis"/>
        <term term="Prüfung"/>
        <term term="Test"/>
    </synset>
        <synset id="5752">
        <categories/>
        <term term="Klassenarbeit"/>
        <term term="Klausur"/>
        <term term="Leistungsüberprüfung"/>
        <term term="Lernerfolgskontrolle"/>
        <term term="Prüfung"/>
        <term term="Schularbeit"/>
        <term term="Schulaufgabe"/>
        <term term="Test"/>
        <term term="Arbeit" level="umgangssprachlich"/>
    </synset>
        <synset id="9138">
        <categories/>
        <term term="Experiment"/>
        <term term="(die) Probe aufs Exempel"/>
        <term term="Probelauf"/>
        <term term="Studie"/>
        <term term="Test"/>
        <term term="Testballon"/>
        <term term="Testlauf"/>
        <term term="Trockenlauf"/>
        <term term="Trockentest"/>
        <term term="Versuch"/>
        <term term="Versuchsballon"/>
    </synset>
    <synset id="6241">
        <categories/>
        <term term="Bewährungsprobe"/>
        <term term="Feuerprobe"/>
        <term term="Feuertaufe"/>
        <term term="harte Prüfung"/>
        <term term="Lackmustest"/>
        <term term="Nagelprobe"/>
        <term term="Test"/>
    </synset>
</matches>

Unfortunately The .php outputs nothing, except of the breaks. I've also tried outputting via print_r, which hasn't helped either. However an if condition with empty($value) showed that $value is not empty.

Upvotes: 0

Views: 45

Answers (1)

Professor Abronsius
Professor Abronsius

Reputation: 33813

I'm not familiar with SimpleXMLElement or any of the SimpleXML family but if you change the following

foreach($syn->synset[0]->term as $value){
    echo $value. "<br>";
}

to

foreach( $syn->synset as $obj ){
    foreach( $obj->term as $arr ){
        foreach( $arr->attributes() as $attr => $value ) echo $attr . ' ' . $value .'<br />';
    }
}

or, more fully:

<?php


    $keyword='kopf'; # for example...

    
    $url = "https://www.openthesaurus.de/synonyme/search?q=". $keyword. "&format=text/xml";
    $options = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'user_agent: my-site.xyz',
        )
    );
    $context = stream_context_create( $options );
    $recive = file_get_contents( $url, false, $context );
    
    
    
    if( ( $recieve = file_get_contents( $url ) )===false ){
        echo "Error fetching XML\n";
    } else {
    
        libxml_use_internal_errors(true);
        $syn = new SimpleXMLElement( $recieve );
        
        if (!$syn) {
            echo "Error loading XML\n";
            foreach(libxml_get_errors() as $error) {
                echo "\t", $error->message;
            }
        } else {
            foreach( $syn->synset as $obj ){
                foreach( $obj->term as $arr ){
                    foreach( $arr->attributes() as $attr => $value ) echo $attr . ' ' . $value .'<br />';
                }
            }
        }
    }
?>

You should get some more meaningful output, such as:

term Haupt
term Kopf
term Caput
level fachsprachlich
term Ballon
level umgangssprachlich
term Birne
level umgangssprachlich
term Denkapparat
level umgangssprachlich
term Denkzentrum
level umgangssprachlich
term Dez
level umgangssprachlich
term Kopp
level umgangssprachlich
term Murmel
level umgangssprachlich
term Nischel
level umgangssprachlich
term Nuss
level umgangssprachlich
term Omme
level umgangssprachlich
term Rübe
level umgangssprachlich
term Schädel
level umgangssprachlich
term Anführer
term Führer
term Häuptling
term Hauptmann
term (führender) Kopf
term Leiter
term Oberhaupt
term Capo (einer Ultra-Gruppe)
level fachsprachlich
term Chef
level umgangssprachlich

Upvotes: 1

Related Questions