Tag Archives: PHP

Posts that include information using the PHP scripting language.

Exec-php WordPress Plugin and PHP 7 Fix

I recently upgraded the version of PHP used on our hosting service to 7.1, as the Elementor plugin requires at least version 7 to run now. This had the side effect of breaking the Exec-php plugin (version 4.9) I use to allow PHP code to run in posts and pages. It manifested itself with the following error message:

Parse error: syntax error, unexpected ‘new’ (T_NEW) in

It appears you can no longer assign a class in PHP version 7 using the ‘&’ symbol and Exec-php makes extensive use of this. Here’s an example:

$GLOBALS['g_execphp_manager'] =& new ExecPhp_Manager();

I went through all of the instances and removed the & symbol using BBEdit and it now runs correctly.

Thanks to the CodeCave blog for this insight!

Sending email with PHPMailer

It appears most of the examples out there relate to version 5.2. The current version is 6 and I had to make the following change in order to get it to work. Everything else from the examples worked.

Change:

$mail = new PHPMailer();

To:

$mail = new PHPMailer\PHPMailer\PHPMailer();

I’ll post (or edit this post) a full example shortly.

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.