Zn4rK / php-withings

PSR-FIG Compatible Withings Body metrics Services API written in PHP.
17 stars 14 forks source link

issue in MeasureCollection class ? #18

Closed toxic0berliner closed 7 years ago

toxic0berliner commented 7 years ago

Hello,

I'm trying to use this API to fetch my withings data and display it using amCharts.js (much better graphs than withings...)

But I need to reverse the results returned by the API (date is DESC and amCharts need it ASC)

I do have a properly validated MeasureCollection, and I tried those 2 functions on it :

$user->getMeasures()->reverse()
$user->getMeasures()->flatten()

Both fail with the same error message : Warning: Invalid argument supplied for foreach() in /volume1/web/private/weight/withings/lib/src/Paxx/Withings/Collection/MeasureCollection.php on line 17

I'll find a very inneficient way to do this in anotherway in the meantime, but if you have any hint on why it does not work, that would be very appreciated.

Also, I've slightly modified the Measure class to include some metrics that were not present, such as mucle mass and hydration mass (as well as dumb functions to get the % of the total mass instead of raw kg measure for those 2). I'm new to git so I'll try to submit a PR if I can find the time.

Best regards,

toxic0berliner.

Zn4rK commented 7 years ago

Hi!

Sorry for the late reply. The MeasureCollection inherits from Laravel's Collection class which in turn has both reverse() and flatten() (and also sortBy('createdAt')!)

Can you please tell me what a var_dump($params) right before the foreach in the MeasureCollection gives you?

So, right here: https://github.com/Zn4rK/php-withings/blob/master/src/Paxx/Withings/Collection/MeasureCollection.php#L16

It looks like Withings has changed something since last time I've used this library.

Zn4rK commented 7 years ago

Oh, and please, try to submit a PR! We were all new once :)

toxic0berliner commented 7 years ago

Hmm...

That var dump contains my full data set... Not only don't I like the idea of sharing that online, but it won't be much readable...

After a bit of digging, it seems that this constructor gets called 3 times in my code (dunno why not only 2... I call $user->getMeasures() only twice in my code.)

After adding the var_dump thing, the webpage that I get looks like this :

<hr/><hr/><hr/><hr/><hr/><pre>array(3) {
  ["updatetime"]=>
  int(1469309611)
  ["measuregrps"]=>
  array(159) {
    <a lot of data here> 
  }
  ["timezone"]=>
  string(12) "Europe/Paris"
}
</pre><hr/><hr/><hr/><hr/><hr/><!DOCTYPE html>
<html>
    <head>
<!-- a lot of my html/js stuff here and then in the middle of my javascript dataProvider definition I get this :-->
<hr/><hr/><hr/><hr/><hr/><pre>array(3) {
  ["updatetime"]=>
  int(1469309611)
  ["measuregrps"]=>
  array(159) {
    <a lot of data here> 
  }
  ["timezone"]=>
  string(12) "Europe/Paris"
}
</pre><hr/><hr/><hr/><hr/><hr/><hr/><hr/><hr/><hr/><hr/><pre>array(159) {
    <a lot of data here but this time it seems different, containing carbon objects whereas before it did not> 
}
</pre><hr/><hr/><hr/><hr/><hr/>
Warning: Invalid argument supplied for foreach() in /volume1/web/private/weight/withings/lib/src/Paxx/Withings/Collection/MeasureCollection.php on line 19

<!-- and finally the rest of my html/js comes here -->

So it seems the $params is not the same the 3 times. Thie first time, I'm comfident that it's whe I first call $user->getMeasures(); The second time, is when I call $user->getMeasures()->sortBy('createdAt') My guess is that the final call passes the dataset (159 data points) but not the expected array of params... But I don't do 3 calls.. I only do 2. Here is the exact line where I call it the second time :

foreach($user->getMeasures()->sortBy('createdAt') as $measure) {

I created some dummy account with real but bogus data and finally got you the whole page for you to look at : http://pastebin.com/0VmjYptj

I hope you find some better answers than I did... I reverted my own code back to not calling anything fancy on this MeasureCollection and instead copying it in an array of my own, and then calling array_reverse.

That turns out to be fine for me anyway since I have to ignore some datapoints coming from withings to replace them with my own previous dataset (cause withings wouldn't let me import muschle and hydration mass despite the fact I had this data..) And I'm tired of having to use the javascript console to check all boxes in the withings website to bulk-delete my failed imports...

Thanks anyway and sorry for this loooong reply.

toxic0berliner commented 7 years ago

Also, I published my ugly attempt at a code here : https://github.com/toxic0berliner/withings-charts

It's a simple chart using amCharts and fetching data using your API.

I feel it does not deserve it's own repo. I'd feel much better if it would be included in the examples section of this api instead. But maybe that wouldn't be such a good idea considering the examples are fetched by composer when using this API... Your decision.

GMetaxakis commented 7 years ago

or Zn4rK can add a reference to your repo(toxic0berliner) inside the examples section

Zn4rK commented 7 years ago

I can't replicate this bug - but I've merged your PR.

I think it's an issue regarding the references used here. If you want, you can try to create a temporary placeholder (instead of the use of a reference) and see if that solves the problem.

I appreciate your effort, and the extract you gave me is fine - I didn't want to make you uncomfortable asking for your measures :) - I wouldn't publish mine either.

Feel free to update the readme and add a link to your repository, and I'll merge that as well.