phparraysloopssimplexml

Loop through array values from SimpleXMLElement


I need to repeat this

do {
    $QUERY = "/member?id=".$counter."&action=refresh";
    $URL = $HTTP.$HTTPUSER.":".$HTTPPASS."@".$HTTPSERVER.":".$HTTPPORT.$QUERY;
    $xml = file_get_contents($URL);
        
    $data = new SimpleXMLElement($xml);
    
    $test_ip = (string)$data->c1;
    
    $dnsip = explode('<br>', $test_ip);
    
    $ext_ip = strip_tags($dnsip[1]);
    
    if ($ext_ip != "127.0.0.1" && $ext_ip != "localhost") {
    
        $dns = strip_tags($dnsip[0]);
    
        echo "$dns $ext_ip <br>";
    }

    $counter +=1;

} while (!empty($data));

as many times as there are values inside an array, so i tried to add this

    $ports  = array('2001','2002','2003'); 

foreach ($ports as $HTTPPORT) {

echo "$HTTPPORT<br>";

$counter = 1;

do {
    $QUERY = "/member?id=".$counter."&action=refresh";
    $URL = $HTTP.$HTTPUSER.":".$HTTPPASS."@".$HTTPSERVER.":".$HTTPPORT.$QUERY;
    $xml = file_get_contents($URL);
        
    $data = new SimpleXMLElement($xml);
    
    $test_ip = (string)$data->c1;
    
    $dnsip = explode('<br>', $test_ip);
    
    $ext_ip = strip_tags($dnsip[1]);
    
    if ($ext_ip != "127.0.0.1" && $ext_ip != "localhost") {
    
        $dns = strip_tags($dnsip[0]);
    
        echo "$dns $ext_ip <br>";
    }

    $counter +=1;

} while (!empty($data)); }

The problem is that it executes the script with only first port number (2001), and I can't discover why.


Solution

  • You could be getting an exception inside your 'do .. while' loop that is causing bother.

    I have added a 'try .. catch' block and some 'echo' statements to ensure it always loops round all the 'ports' now. Changed 'catch' to mark $data as empty then continue.

    here is tested code:

    <?php
    
    $ports  = array('2001','2002','2003');
    
    $counter = 0; // total count of documents
    
    foreach($ports as $HTTPPORT) {
    
        echo $HTTPPORT, ' start of process port loop<br/>';
    
        try { // catch any error -- report it and loop round again
            do {
                $QUERY = "/member?id=".$counter."&action=refresh";
                $URL = ''; // $HTTP.$HTTPUSER.":".$HTTPPASS."@".$HTTPSERVER.":".$HTTPPORT.$QUERY;
                try {
                    $xml = file_get_contents($URL);
                    $data = new SimpleXMLElement($xml);
                }
                catch (Exception $e) { // ignore any errors
                    echo 'SimpleXMLElement : oops :', $e->getMessage(), '<br />';
                    $data = ''; // mark as empty
                }
    
                if (!empty($data)) { // process data
                    $test_ip = (string)$data->c1;
                    $dnsip = explode('<br>', $test_ip);
    
                    $ext_ip = strip_tags($dnsip[1]);
    
                    if ($ext_ip != "127.0.0.1" && $ext_ip != "localhost") {
    
                        $dns = strip_tags($dnsip[0]);
    
                        echo "$dns $ext_ip <br>";
                    }
                }
    
                $counter +=1;
    
            } while (!empty($data));
    
        } // end of try to get and process a document...
        catch (Exception $e) { // catch all errors for now
            echo 'Processing List of Ports: oops! :', $e->getMessage(), '<br />';
        }
    } // end of foreach port