PHP code to upload a GPX file

495 views
Skip to first unread message

Matt Stuehler

unread,
Jul 22, 2014, 4:55:12 PM7/22/14
to strav...@googlegroups.com
All,

I'm working on a PHP application to upload GPX files to Strava.

So far - I've got the oath part working just fine - I can get the access token.

Here's the relevant part of my code:

// $filename is the name of the GPX file
// $actual_file contains the full path

$actual_file = realpath($filename);
$postdata = "activity_type=ride&file=". "@" . $actual_file . ";filename=" . $filename . "&data_type=gpx";

$headers = array('Authorization: Bearer ' . $strava_access_token);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_POST, 3);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec ($ch);

When I execute this - here's what I get in response:

{"message":"Bad Request", "errors":[{"resource":"Upload", "field":"file","code":"not a file"}]}

Any help or insight would be much appreciated!




Paul Mach

unread,
Jul 22, 2014, 5:00:51 PM7/22/14
to Matt Stuehler, strava-api
Maybe some php experts can comment further, but I've gotten this to work in the past:

$post = array(
        "activity_type" => "ride",
        "data_type" => "gpx",
        "filename" => $filename,
        "data" => file_get_contents($actual_file)
);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

Dr. Paul Mach
STRAVA


--
You received this message because you are subscribed to the Google Groups "Strava API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to strava-api+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matt Stuehler

unread,
Jul 22, 2014, 5:36:32 PM7/22/14
to strav...@googlegroups.com, stuehl...@gmail.com
Paul,

Thank you for you help!

Unfortunately - that doesn't seem to fix my problem.

If I use other API functions (e.g., https://www.strava.com/api/v3/athlete) - it works great. So, I know that I've at least got the authentication part right.

It seems like the main thing I'm getting wrong is setting the "file" parameter...

Matt Stuehler

unread,
Jul 23, 2014, 9:11:55 AM7/23/14
to strav...@googlegroups.com
OK - thanks to help from Paul, I've got this working. I don't know if this code is ideal, but it works...

// $filename is the name of the file
// $actual_file includes the filename and the full path to the file
// $strava_access_token contains the access token

$actual_file = realpath($filename);


$postfields = array(
    "activity_type" => "ride",
    "data_type" => "gpx",
    "file" => '@' . $actual_file . ";type=application/xml"
);

$headers = array('Authorization: Bearer ' . $strava_access_token);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

$response = curl_exec ($ch);

Apparently, it's important not to include the CURLOPT_POST option.
Reply all
Reply to author
Forward
0 new messages