1
我正在處理一個應用程序,我希望能夠使用它的API v3將活動(GPX文件)上傳到Strava。使用PHP上傳活動到Strava使用API v3
我的應用程序成功地處理了OAuth過程 - 我可以成功地請求活動等。
但是,當我嘗試上傳活動時 - 失敗。
這裏是我的代碼相關的示例:
// $filename is the name of the GPX file
// $actual_file contains the full path
$actual_file = realpath($filename);
$url="https://www.strava.com/api/v3/uploads";
$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);
這裏就是我在響應得到:
{"message":"Bad Request", "errors":[{"resource":"Upload", "field":"file","code":"not a file"}]}
然後我嘗試這樣做:
// $filename is the name of the GPX file
// $actual_file contains the full path
$actual_file = realpath($filename);
$url="https://www.strava.com/api/v3/uploads";
$postfields = array(
"activity_type" => "ride",
"data_type" => "gpx",
"file" => "@" . $filename
);
$postdata = http_build_query($postfields);
$headers = array('Authorization: Bearer ' . $strava_access_token, "Content-Type: application/octet-stream");
$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, count($postfields));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$fp = fopen($filename, 'r');
curl_setopt($ch, CURLOPT_INFILE, $fp);
$json = curl_exec ($ch);
$error = curl_error ($ch);
這裏就是我進去迴應:
{"message":"Bad Request", "errors":[{"resource":"Upload", "field":"data","code":"empty"}]}
很明顯,我在嘗試傳遞GPX文件時做錯了什麼。
是否可以提供一些示例PHP代碼來展示這應該如何工作?
對於它的價值 - 我相當肯定GPX文件是有效的(它實際上是我使用Strava的導出功能下載的文件)。
你能解釋一下這個更多..?我在Android中獲得相同的響應(代碼:不是文件) – Ewoks