1

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.

4
  • Maybe you get stuck in your do while loop. Is $data ever empty? Commented Mar 31, 2014 at 8:36
  • Yes, after it finishes the while loop for port 2001 it exits normally, but doesn't continue to do the same with other 2 ports...really don't know what is happening, i can't find any errors Commented Mar 31, 2014 at 8:38
  • What language is this, PHP? Please add the appropriate language tag. Commented Mar 31, 2014 at 8:38
  • Amended code to do more sensible things now. Commented Apr 6, 2014 at 13:17

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

This works, the thing is when while loop gets to the end of one loop, it reports an error instead of breaking the loop and continuing with another port...is there a better way to loop through id's untill it gets no contents from a page?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.