PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, January 10, 2022

[FIXED] php json multidimentional array output

 January 10, 2022     json, php, phpmyadmin     No comments   

Issue

I am trying to create api to show my servers status

recently I created a api which was very rough you may laugh too much here is a code what I did

            $Status2 = "Failed";
            $Status3 = "Failed";
            $Status4 = "Failed";
            
            $info1 = "Opps! looks like our Server One is Down. Please contact our support team for more information";
            $info2 = "Opps! looks like our Server Two is Down. Please contact our support team for more information";
            $info3 = "Opps! looks like our Server Three is Down. Please contact our support team for more information";
            $info4 = "Opps! looks like our Server Four is Down. Please contact our support team for more information";
            
            $serverOne = "http://firstserver.com";
            $serverTwo = "http://2ndserver.com";
            $serverThree = "http://3rdserver.com";
            $serverFour = "http://4thserver.com";
            
            // Takes raw data from the request
            $jsonOne = file_get_contents($serverOne);
            $jsonTwo = file_get_contents($serverTwo);
            $jsonThree = file_get_contents($serverThree);
            $jsonFour = file_get_contents($serverFour);

            // Converts it into a PHP object            
            $serverOneBody = @json_decode($jsonOne, true);
            $serverTwoBody = @json_decode($jsonTwo, true);
            $serverThreeBody = @json_decode($jsonThree, true);
            $serverFourBody = @json_decode($jsonFour, true);
            
            $statusOne = $serverOneBody["status"];
            $statusTwo = $serverTwoBody["status"];
            $statusThree = $serverThreeBody["status"];
            $statusFour = $serverFourBody["status"];
            
            if($statusOne == 1){$Status1 = "Success"; $info1 = "Success! Server One is live and working!";}
            if($statusTwo == 1){$Status2 = "Success"; $info2 = "Success! Server Two is live and working!";}
            if($statusThree == 1){$Status3 = "Success"; $info3 = "Success! Server Three is live and working!";}
            if($statusFour == 1){$Status4 = "Success"; $info4 = "Success! Server Three is live and working!";}
            
            $respon = array(    "serverOne" => array ('response' => 200,'status' => $Status1,'info' => $info1),
                                "serverTwo" => array ('response' => 200,'status' => $Status2,'info' => $info2),
                                "serverThree" => array ('response' => 200,'status' => $Status3,'info' => $info3),
                                "serverFour" => array ('response' => 200,'status' => $Status4,'info' => $info4)
                            );
                                
$this->response($this->json($respon), 200);

i know its very raw.

I want to receive a json in

{
        "serverOne": {
          "response": 200,
          "status": "Success",
          "info": "Success! Server One is live and working!"
        },
        "serverTwo": {
          "response": 200,
          "status": "Success",
          "info": "Success! Server Two is live and working!"
        },
        "serverThree": {
          "response": 200,
          "status": "Success",
          "info": "Success! Server Three is live and working!"
        },
        "serverFour": {
          "response": 200,
          "status": "Success",
          "info": "Success! Server Four is live and working!"
        }
    }     

Now I am trying

$items = array();
            
            $servers = array(
            "one" => "sv1.com", 
            "two" => "sv2.com",
            "three" => "sv3.com"
            ); 

            foreach ($servers as $server) {
                $json = file_get_contents($server);
                // Converts it into a PHP object            
                $jsonBody = @json_decode($json, true);
                $status = $jsonBody["status"];
                
                if($status == 1){
                    array_push($items,"Success");
                    array_push($items,"Success! Server number is live and working!");
                    }else{                  
                    array_push($items,"Error");
                    array_push($items,"Error! Server number is Not working properly!");                 
                }               

                
                
            }
            $this->response($this->json($items), 200);

but its only looping its not showing server one two and three like.

How can I get the possible output in above given json format?

Please help me I searched everywhere on internet but didn't find the answer.

with my current code I am receiving

[
    "Success",
    "Success! Server number is live and working!",
    "Success",
    "Success! Server number is live and working!",
    "Error",
    "Error! Server number is Not working properly!",
    "Success",
    "Success! Server number is live and working!"
]


Solution

After long time i finally got it.

$respon = array();

foreach (array_values($servers) as $id => $url) {
        $json = file_get_contents($url);
        // Converts it into a PHP object            
        $jsonBody = @json_decode($json, true);
        $status = $jsonBody["status"];
                        
    if($status=='1'){
       $respon[$id]= array("response"=>"$headers[0]","status"=>"Success","info"=>"Server $id is Live and Working Fine!");
        }else{
        $respon[$id]= array("response"=>"$headers[0]","status"=>"Failed","info"=>"Server $id is not Working!");
        }
    }

It was not easy for me as a beginner to think like this but I finally understand it correctly.

If anyone in future need help use it as a reference.



Answered By - Pushkraj Jori
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing