从PHP中的SimpleXML对象中检索值

时间:2016-02-29 09:59:43

标签: php arrays xml simplexml bigbluebutton

$bbb = new BigBlueButton();

$recordingsParams = array(
    'meetingId' => '', 

);

// Now get recordings info and display it:
$itsAllGood = true;
try {$result = $bbb->getRecordingsWithXmlResponseArray($recordingsParams);}
    catch (Exception $e) {
        echo 'Caught exception: ', $e->getMessage(), "\n";
        $itsAllGood = false;
    }

    if ($itsAllGood == true) {
        if ($result == null) {
            echo "Failed to get any response. Maybe we can't contact the BBB server.";
        }   
        else { 

         if ($result['returncode'] == 'SUCCESS') {
                echo "<p>Meeting info was found on the server.</p>";
            }


            else {
                echo "<p>Failed to get meeting info.</p>";
            }
            print_r($result);

        }
    }

当我使用Bigbluebutton时,我需要从这些XML格式响应中检索特定数据。我不知道如何从这些XML格式中检索特定数据?

Array
(
    [returncode] => SimpleXMLElement Object
        (
            [0] => SUCCESS
        )

    [0] => Array
        (
            [meetingId] => SimpleXMLElement Object
                (
                    [0] => newtech
                )                       
         )

    [1] => Array
        (
            [meetingId] => SimpleXMLElement Object
                (
                    [0] => menew
                )                          
        )  
)

我需要显示meetingId的值。

1 个答案:

答案 0 :(得分:0)

如果您想使用数组,也可以强制转换XML。此函数返回一个数组,其中包含有关调用时服务器上正在发生的情况的信息(仍然非常欢迎改进):



    function getServerInfo() {
        $url="https://you-bbb-server-api-getMeetings-call-url";
        $a= (array)new SimpleXMLElement(file_get_contents($url));
        if($a["returncode"]=="SUCCESS") {
            if($a["meetings"]) {
               foreach($a["meetings"] as $meeting) {
                    $meeting=(array)$meeting;
                    $data["Meetings"][$meeting["meetingID"]]["Teilnehmer"]=$meeting["participantCount"];
                    $data["Meetings"][$meeting["meetingID"]]["Name"]=$meeting["meetingName"];
                    $data["Gesamt"]+=(int)$meeting["participantCount"];
                    $data["Anzahl"]++;
                    $data["Meetings"][$meeting["meetingID"]]["Video"]=$meeting["videoCount"];
                    $data["Meetings"][$meeting["meetingID"]]["Start"]=date('d.m.Y H:i:s',floor($meeting["startTime"]/1000));
                    if($meeting["attendees"]) foreach($meeting["attendees"] as $att) {
                        $att=(array)$att;
                        if($att["role"]=="MODERATOR") $data["Meetings"][$meeting["meetingID"]]["Liste"]["M"]=array($att["fullName"],($att["hasJoinedVoice"]=="true")? 1:0,($att["hasVideo"]=="true")? 1:0);
                        else $data["Meetings"][$meeting["meetingID"]]["Liste"]["T"][]=array($att["fullName"],($att["hasJoinedVoice"]=="true")? 1:0,($att["hasVideo"]=="true")? 1:0);
                    }
                }
                return($data);
            } else return(array("Anzahl"=>0,"Gesamt"=>0));
        } else return(-1);
    }