Monthly Archives: March 2015

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.