Parsing Strava’s GPX File in PHP

I love Strava! However, as with all tracking sites, Strava doesn’t always provide the exact window I want into my data. I’ve been thinking about building an app that allows me to manipulate the specific data I’m interested in but to do that, one has to get the data first. Given that, I’ve been playing around with parsing Strava GPX files. I’ve been looking at Garmin GPX and FIT files as well, but the Strava ones seem the easiest to work with.

The challenge I had with the Strava GPX file was getting at the data in the extensions: temperature (atemp), heart rate (hr) and Cadence (cad) data. It took me a few hours to figure out how to parse those values (I’m not very good at XML, namespaces, etc.), so I wanted to post this bit of PHP code to hopefully save others some time!

foreach($stravaData->trk->trkseg->children() as $trkpts) {
    $dataPoint->trkpt = $i;
    $dataPoint->latitude = $trkpts['lat'];
    $dataPoint->longitude = $trkpts['lon'];
    $dataPoint->elevation = $trkpts->ele;
    $dataPoint->time = $trkpts->time;
    $dataPoint->temp = $trkpts->extensions->children('gpxtpx', true)->TrackPointExtension->atemp;
    $dataPoint->hr = $trkpts->extensions->children('gpxtpx', true)->TrackPointExtension->hr;
    $dataPoint->cad = $trkpts->extensions->children('gpxtpx', true)->TrackPointExtensi
 on->cad;
    ...
}

The above code assumes you have created a “dataPoint” class to store the relevant Strava data (trackpoint number, latitude, longitude, elevation, time, temp, hr, cad, … you may have others like power). Let me know if you have any questions.

3 thoughts on “Parsing Strava’s GPX File in PHP

    1. Norm Post author

      Hi Tarique,

      As it turns out, I haven’t moved forward with parsing Strava GPX files in PHP. I use a Garmin Forerunner 920XT as my workout watch and some of the information/data I wanted is not transmitted to Strava and is only available via the Garmin files. So, I’ve resorted to with those.

      Is there something specific you were trying to do?

      Norm

      Reply
  1. Pingback: Parsing Strava GPX File in PHP – Moving Posts

Leave a Reply to Norm Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.