JenKumar JenKumar - 4 months ago 11
JSON Question

JSON to PHP parsing not working as intended

I am trying to retrieving data from the JSON URL into PHP webpage. The original URL is:

http://reverseip.logontube.com/?url=104.28.23.18&output=json

The URL returns the host ip and all domains hosted on that IP.

Now using JSON online tool I tried to decode the output. The output is as follows:

stdClass Object ( [hostip] => 104.28.23.18 [hostname] => 104.28.23.18 [response] => stdClass Object ( [domain_count] => 14 [domains] => Array ( [0] => th998877.com [1] => careerplanningbooks.com [2] => humanhangbag.com [3] => azbabycarez.net [4] => travellersrights.com [5] => big-booobs-pics.net [6] => enddepression.us [7] => alumnihighschool.net [8] => rubilax.com [9] => xtcts.com [10] => car-hoken-navi.com [11] => lightpostlocalexperts.com [12] => dublo.net [13] => 123savemp3.info ) ) )


The above is the raw data.
Then I used a PHP function to display that data on webpage. This is the PHP script is used:

$string = file_get_contents("http://reverseip.logontube.com/?url=104.28.23.18&output=json");
$json = json_decode($string, true);

foreach ($json as $key => $value) {
if (!is_array($value)) {
echo $key . '=>' . $value . '<br />';
} else {
foreach ($value as $key => $val) {
echo $key . '=>' . $val . '<br />';
}
}
}


And this is the output I got from PHP which is displayed in my webpage:

hostip=>104.28.23.18

hostname=>104.28.23.18

domain_count=>14

domains=>Array


My Question is: In the last line above, why domains is returning the word Array? I was expecting the name of each domain like th998877.com careerplanningbooks.com, etc. How can I display those domains in PHP using the original JSON url?

Answer

You need to make new foreach

<?php
$string = file_get_contents("http://reverseip.logontube.com/?url=104.28.23.18&output=json");
$json = json_decode($string, true);

foreach ($json as $key => $value) {
    if (!is_array($value)) {
    echo $key . '=>' . $value . '<br />';
    } else {
        foreach ($value as $key => $val) {

            //Add this to your code

            if(is_array($val)){
                foreach($val as $array => $i){
                    echo $i . '<br />';
                }
            }
            else{
                echo $val . '<br />';
            }


        }
    }
}
Comments