Parsing JSON data and it's objects into different PHP functions -


i've been looking around hoping find issue i'm stumbling upon... couldn't find it. i've made class in php, within class there function connects api retrieving json data (this function has been tested , works charm). i'm trying seperate objects of json data receive different functions.

i've managed parse data through foreach , echo, it'd hassle constantly.

this looks idea.

edit

issue has been resolved, public function foo(); had , $info variable had json_decode(); interupted next function split data.

class parsedata{      var $name;     var $domain;      public function foo($name, $domain)      {          $this->name = $name;         $this->domain = $domain;          $ch = curl_init();          $user_agent = 'mozilla/5.0 (x11; linux i686) applewebkit/537.31 (khtml, gecko) chrome/26.0.1410.43 safari/537.31';          $request_headers = array();         $request_headers[] = 'user-agent: ' . $user_agent;         $request_headers[] = 'accept: text/html,application/xhtml+xml,application/xml,application/json;q=0.9,*/*;q=0.8';          curl_setopt($ch, curlopt_url, "https://www.xxx." . $domain . "/api/public/users?name=" . $name . "");         curl_setopt($ch, curlopt_header, false);         curl_setopt($ch, curlopt_returntransfer, true);         curl_setopt($ch, curlopt_httpheader, $request_headers);         curl_setopt($ch, curlopt_followlocation, true);         curl_setopt($ch, curlopt_ssl_verifyhost, 0);         curl_setopt($ch, curlopt_ssl_verifypeer, 0);          curl_setopt($ch, curlopt_url, "https://www.xxx." . $domain . "/api/public/users?name=" . $name);          $id = json_decode(curl_exec($ch));          if (isset($id) && $id->profilevisible == 1) {              curl_setopt($ch, curlopt_url, "https://www.xxx." . $domain . "/api/public/users/" . $id->uniqueid . "/profile");             $info = curl_exec($ch);          } else             $info = false;          curl_close($ch);          return $info;      }      public $response = array(), $user = array(), $friends = array(), $groups = array(), $rooms = array(), $badges = array();     public function __construct(){         $this->response = $this->foo("user", "nl");         $this->init();     }      // function split data sub variables     private function init(){         $this->response =json_decode(file_get_contents($this->response), true);         $this->user = $this->response['user'];         $this->friends = $this->response['friends'];         $this->groups = $this->response['groups'];         $this->rooms = $this->response['rooms'];         $this->badges = $this->response['badges'];         // free main response object         unset($this->response);     }      public function uid(){         echo $this->user['uniqueid'];     }      public function name(){         echo $this->user['name'];     }      public function membersince(){         echo $this->user['membersince'];     }      public function motto(){         echo $this->user['motto'];     }      public function figure(){         echo $this->user['figurestring'];     }      //this function manipulate user data.     public function user(){         echo "naam: ".$this->user['name'].'<br/>';         echo "motto: ".$this->user['motto'].'<br/>';         echo "lid sinds: ".$this->user['membersince'].'<br/>';         echo "unique id: ".$this->user['uniqueid'].'<br/>';         echo "figurestring: ".$this->user['figurestring'].'<br/>';         foreach($this->user['selectedbadges'] $selectedbadge){             echo 'badge index: '. $selectedbadge['badgeindex'];             echo 'badge code: '. $selectedbadge['code'];             echo 'badge naam: '. $selectedbadge['name'];             echo 'badge beschrijving: '. $selectedbadge['description'];         }     }     public function friends(){         //do stuff $this->friends.     }     public function groups(){         //do stuff $this->groups     }     //and other functions badges , rooms etc. }  $parser = new parsedata(); $parser->user(); 

the api sends different objects , i'd seperate them different functions "user", "friends", "groups" etc. in order retrieve strings out of objects. question is, possible parse api data through different functions within same class? if so, how do this? , calling function easier task or hassle doing foreach method?

so, i've looked json , can this

class parsedata{   public $response = array(), $user = array(), $friends = array(), $groups = array(), $rooms = array(), $badges = array();   public function __construct(){     $this->response = call_your_function_get_api_response();     $this->init();   }   // function split data sub variables   private function init(){     $this->response = json_decode(file_get_contents('json.json'), true);     //i'm using file contents here, i've stored json file.     // you'll have     //$this->response = json_decode($this->response, true);     $this->user = $this->response['user'];     $this->friends = $this->response['friends'];     $this->groups = $this->response['groups'];     $this->rooms = $this->response['rooms'];     $this->badges = $this->response['badges'];     // free main response object     unset($this->response);   }   /*     * i'm updating here new function name(), print name this.   */   public function name(){     echo "name: ".$this->user['name'].'<br/>'; //this print name only.   }   //this function manipulate user data.   public function user(){     echo "user's unique id: ".$this->user['uniqueid'].'<br/>';     echo "name: ".$this->user['name'].'<br/>';     echo "figurestring: ".$this->user['figurestring'].'<br/>';     foreach($this->user['selectedbadges'] $selectedbadge){         echo 'badge index: '. $selectedbadge['badgeindex'];         echo 'badge code: '. $selectedbadge['code'];         echo 'badge name: '. $selectedbadge['name'];         echo 'badge description: '. $selectedbadge['description'];     }   }   public function friends(){     //do stuff $this->friends.   }   public function groups(){     //do stuff $this->groups   }   //and other functions badges , rooms etc. } $parser = new parsedata(); $parser->user();  /*  * , can call function  * $parser->friends();      process friends data json response  * $parser->groups();       process groups data json response.  * ... rest of functions ... */ 

i've tested 1 iteration of stuff using users() function.

here output

enter image description here

edit

i've updated class new method name() print name,

$parser->name(); 

another edit

as per comment

i'm putting __construct() code here again.

public function __construct(){   $get = new foo();   $this->response = $get->bar("person", "nl");   $this->init(); } 

and rest of things same.

$parser = new parsedata(); $parser->user(); $parser->name();  //will display name only. 

putting together

you need update 2 functions

public function __construct(){     $this->response = json_decode($this->foo("user", "nl"), true);     $this->init(); } 

and in init() comment out first line it's not needed more.

hope you.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -