Gaurav Mehta
Gaurav Mehta

Reputation: 47

Reading API response in PHP

I am making a request to mixpanel API

$data = $mp->request(array('segmentation'), array(
    'event' => 'Loaded a Page',
    'name' => 'Loaded a Page',
    'from_date' => '2015-07-01',
    'to_date' => '2015-07-01',
    'type' => 'unique',   
    'expire' => '1439089200'
    //'unit' => 'day',
    //'interval' => '7',
    //'format' => 'json'
));
//echo $data;
var_dump($data);

Here is the response I am getting.

object(stdClass)#2 (2) { ["legend_size"]=> int(1) ["data"]=> object(stdClass)#3 (2) 
{ ["series"]=> array(1) { [0]=> string(10) "2015-07-01" } ["values"]=> object(stdClass)#4 (1) 
{ ["Loaded a Page"]=> object(stdClass)#5 (1) { ["2015-07-01"]=> int(267691) } } } }

Can anyone please help me with how can I read the response in variables. Say I want to get the count of Loaded a Page event for '2015-07-01' from the response.

Upvotes: 0

Views: 384

Answers (1)

Jason McCreary
Jason McCreary

Reputation: 72981

Say I want to get the count of Loaded a Page event for '2015-07-01' from the response.

$data->values->{"Loaded a Page"}->{"2015-07-01"}

Please note, this is a very nasty format. I would check if request() takes parameters to adjust the format.

Nonetheless, I'd encourage you to read about Objects Properties in PHP and this answer Accessing Class Properties with Spaces.

Upvotes: 1

Related Questions