1

I've been looking around hoping I could find the issue I'm stumbling upon... However I couldn't find it. I've made a class in PHP, within that class there is a function that connects to an API retrieving JSON data (this function has been tested and works like a charm). However now I'm trying to seperate the objects of the JSON data I receive into different functions.

I've only managed to parse the data now through a foreach and echo, but it'd be a hassle to do it like that constantly.

This is what it currently looks like so you get an idea.

EDIT

Issue has been resolved, the public function foo(); Had and $info variable which had a json_decode(); which interupted the next function to split the 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, like 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();
    }

    // this function will split data into 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 will 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'] as $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 with $this->friends.
    }
    public function groups(){
        //do stuff with $this->groups
    }
    //and other functions like badges and rooms etc.
}

$parser = new parseData();
$parser->user();

The API sends different objects back and I'd like to seperate them into different functions as "user", "friends", "groups" etc. in order to retrieve all the strings out of those objects. So my question is, is it possible to parse API data through different functions within the same class? If so, how do I do this? And will calling the function be an easier task to do as well or will it just be a hassle like doing the foreach method?

9
  • please include snippet of your output as well Commented Nov 1, 2015 at 17:37
  • Output of the JSON data? Commented Nov 1, 2015 at 17:53
  • yes, Output of the JSON data Commented Nov 1, 2015 at 17:56
  • To keep the person anonymous, I've edited a few outcomes, but the structure is the same. {"user":{"uniqueId":"aaaa-662628f1fb34cbf4346103dff1685508","name":"foo","slogan":"Safety has no time-out!","memberSince":"2004-07-08T04:56:03.000+0000","profileVisible":true,"lastWebAccess":null} Commented Nov 1, 2015 at 18:06
  • you want to separate the response on the base of main key {"user":, right? as there could be "friends", "groups" etc. and you want to call from specific function, like if user, call user(), if friends, call friends(), right? Commented Nov 1, 2015 at 18:11

1 Answer 1

1

So, I've looked into your json and you can like 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();
  }
  // this function will split data into sub variables
  private function init(){
    $this->response = json_decode(file_get_contents('json.json'), TRUE);
    //I'm using file get contents here, as I've stored your json into file.
    // but you'll have to do like this
    //$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 with new function name(), so to print name only do this.
  */
  public function name(){
    echo "Name: ".$this->user['name'].'<br/>'; //This will print name only.
  }
  //this function will 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'] as $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 with $this->friends.
  }
  public function groups(){
    //do stuff with $this->groups
  }
  //and other functions like badges and rooms etc.
}
$parser = new parseData();
$parser->user();

/*
 * and you can call function like this
 * $parser->friends();      will process only friends data from json response
 * $parser->groups();       will process only groups data from json response.
 * ... rest of functions ...
*/

I've tested this with one iteration of stuff using users() function.

here is output

enter image description here

Edit

I've updated class with a new method name() so now to print only name,

$parser->name();

Another Edit

as per your 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 the things are same.

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

putting all together

you need to update two functions

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

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

Hope this will help you.

Sign up to request clarification or add additional context in comments.

12 Comments

Thanks! Though I have one question, what if for example I need to echo "name" only? Is that possible as well and if I'm correct, will this be a class apart or do they have to be in the same class?
I didn't understand the last part actually, but, all these functions will be in the same class. and to print name only, I'm updating my answer :)
Basically, I had the response function in class "foo", but what I tried to ask is if that function needed to be in the ParseData function. However I already answered that question to myself which was a silly question. Thanks for the push man, I appreciate it.
it's up to you, wherever you want to put that function. if it's in the foo class, then in this class, you can do like this, in __construct() method, add this line. $get = new foo(); $this->response = $get->response();
Okay, now it seems to work, I have extended your class with the one where just the API response is in. However, how can I test it now? Because the function within the API class still relies on two variables. ($name, $domain). Do I do this: $get = new foo(); $getdata = $get->bar("person", "nl"); $parser = new parseData($getdata); $parser->user();
|

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.