diff --git a/.gitignore b/.gitignore index a403a60..56c4497 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ .DS_Store /.idea/ + +/vendor/ + +/examples/ diff --git a/README.md b/README.md index 2013566..88cfa87 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,17 @@ StravaApi Simple PHP class to interact with Strava's V3 API. +VERSION BUMP +------- + +Latest version **1.0** + +Updates include: + +- Better composer support +- PSR 2 standards +- New `setAccessToken()` method + Overview ------------ @@ -22,7 +33,7 @@ Add `iamstuartwilson/strava` to your `composer.json`: ``` json { "require" : { - "iamstuartwilson/strava" : "dev-master" + "iamstuartwilson/strava" : "1.0.*" } } ``` @@ -38,34 +49,37 @@ Include the class and instantiate with your **client_id** and **client_secret** require_once 'StravaApi.php'; - $api = new StravaApi( $clientId, $clientSecret ); + $api = new Iamstuartwilson\StravaApi( + $clientId, + $clientSecret + ); You will then need to [authenticate](http://strava.github.io/api/v3/oauth/) your strava account by requesting an access code *[1]*. You can generate a URL for authentication using the following method: - $api->authenticationUrl( $redirect, $approvalPrompt = 'auto', $scope = null, $state = null ); + $api->authenticationUrl($redirect, $approvalPrompt = 'auto', $scope = null, $state = null); When a code is returned you must then exchange it for an [access token](http://strava.github.io/api/v3/oauth/#post-token) for the authenticated user: - $api->tokenExchange( $code ); + $api->tokenExchange($code); Example Requests ------------ Get the most recent 100 KOMs from any athlete - $api->get( 'athletes/:id/koms', $accessToken, array( 'per_page' => 100 ) ); + $api->get('athletes/:id/koms', array('per_page' => 100)); Post a new activity *[2]* - $api->post( 'activities', $accessToken, array( 'name' => 'API Test', 'type' => 'Ride', 'start_date_local' => date( 'Y-m-d\TH:i:s\Z' ), 'elapsed_time' => 3600 ) ) ); + $api->post('activities', array('name' => 'API Test', 'type' => 'Ride', 'start_date_local' => date( 'Y-m-d\TH:i:s\Z'), 'elapsed_time' => 3600)); Update a athlete's weight *[2]* - $api->put( 'athlete', $accessToken, array( 'weight' => 70 ) ); + $api->put('athlete', array('weight' => 70)); Delete an activity *[2]* - $api->delete( 'activities/:id', $accessToken ); + $api->delete('activities/:id'); ###Notes diff --git a/StravaApi.php b/StravaApi.php index aeeee17..ef56284 100644 --- a/StravaApi.php +++ b/StravaApi.php @@ -1,244 +1,284 @@ - * @link https://github.com/iamstuartwilson/strava - * @since 18/02/2014 - */ - - class StravaApi{ - - /** - * Sets up the class with the $clientId and $clientSecret - * @param int $clientId - * @param string $clientSecret - */ - - public function __construct( $clientId, $clientSecret ){ - - $this->clientId = $clientId; - $this->clientSecret = $clientSecret; - $this->baseUrl = 'https://www.strava.com/'; - $this->apiUrl = $this->baseUrl . 'api/v3/'; - $this->authUrl = $this->baseUrl . 'oauth/'; - - } - - /** - * Appends query array onto URL - * @param string $url - * @param array $query - * @return string - */ - - protected function parseGet( $url, $query ){ - - $append = strpos( $url, '?' ) === false ? '?' : '&'; - - return $url . $append . http_build_query( $query ); - - } - - /** - * Parses JSON as PHP object - * @param string $response - * @return object - */ - - protected function parseResponse( $response ){ - - return json_decode( $response ); - - } - - /** - * Makes HTTP Request to the API - * @param string $url - * @param array $parameters - * @return mixed - */ - - protected function request( $url, $parameters = array(), $request = false ){ - - $this->lastRequest = $url; - $this->lastRequestData = $parameters; - $curl = curl_init( $url ); - $curlOptions = array( - CURLOPT_SSL_VERIFYPEER => false, - CURLOPT_FOLLOWLOCATION => true, - CURLOPT_REFERER => $url, - CURLOPT_RETURNTRANSFER => true - ); - - if( ! empty( $parameters ) || ! empty( $request ) ){ - - if( ! empty( $request ) ){ - - $curlOptions[ CURLOPT_CUSTOMREQUEST ] = $request; - $parameters = http_build_query( $parameters ); - - } - - else{ - - $curlOptions[ CURLOPT_POST ] = true; - - } - - $curlOptions[ CURLOPT_POSTFIELDS ] = $parameters; - - } - - curl_setopt_array( $curl, $curlOptions ); - $response = curl_exec( $curl ); - $error = curl_error( $curl ); - $this->lastRequestInfo = curl_getinfo( $curl ); - curl_close( $curl ); - - if( ! $response ){ - - return $error; - - } - - else{ - - return $this->parseResponse( $response ); - - } - - } - - /** - * Creates authentication URL for your app - * @param string $redirect - * @param string $approvalPrompt - * @param string $scope - * @param string $state - * @link http://strava.github.io/api/v3/oauth/#get-authorize - * @return string - */ - - public function authenticationUrl( $redirect, $approvalPrompt = 'auto', $scope = null, $state = null ){ - - $parameters = array( - 'client_id' => $this->clientId, - 'redirect_uri' => $redirect, - 'response_type' => 'code', - 'approval_prompt' => $approvalPrompt, - 'scope' => $scope, - 'state' => $state - ); - - return $this->parseGet( $this->authUrl . 'authorize', $parameters ); - - } - - /** - * Authenticates token returned from API - * @param string $code - * @link http://strava.github.io/api/v3/oauth/#post-token - * @return function - */ - - public function tokenExchange( $code ){ - - $parameters = array( - 'client_id' => $this->clientId, - 'client_secret' => $this->clientSecret, - 'code' => $code - ); - - return $this->request( $this->authUrl . 'token', $parameters ); - - } - - /** - * Deauthorises application - * @param string $accessToken - * @link http://strava.github.io/api/v3/oauth/#deauthorize - * @return function - */ - - public function deauthorize( $accessToken ){ - - $parameters = array( - 'access_token' => $accessToken - ); - - return $this->request( $this->authUrl . 'deauthorize', $parameters ); - - } - - /** - * Sends GET request to specified API endpoint - * @param string $request - * @param string $accessToken - * @param array $parameters - * @example http://strava.github.io/api/v3/athlete/#koms - * @return function - */ - - public function get( $request, $accessToken, $parameters = array() ){ - - $parameters = array_merge( $parameters, array( 'access_token' => $accessToken ) ); - $requestUrl = $this->parseGet( $this->apiUrl . $request, $parameters ); - - return $this->request( $requestUrl ); - - } - - /** - * Sends PUT request to specified API endpoint - * @param string $request - * @param string $accessToken - * @param array $parameters - * @example http://strava.github.io/api/v3/athlete/#update - * @return function - */ - - public function put( $request, $accessToken, $parameters = array() ){ - - $parameters = array_merge( $parameters, array( 'access_token' => $accessToken ) ); - - return $this->request( $this->apiUrl . $request, $parameters, 'PUT' ); - - } - - /** - * Sends POST request to specified API endpoint - * @param string $request - * @param string $accessToken - * @param array $parameters - * @example http://strava.github.io/api/v3/activities/#create - * @return function - */ - - public function post( $request, $accessToken, $parameters = array() ){ - - $parameters = array_merge( $parameters, array( 'access_token' => $accessToken ) ); - - return $this->request( $this->apiUrl . $request, $parameters ); - - } - - /** - * Sends DELETE request to specified API endpoint - * @param string $request - * @param string $accessToken - * @param array $parameters - * @example http://strava.github.io/api/v3/activities/#delete - * @return function - */ - - public function delete( $request, $accessToken, $parameters = array() ){ - - $parameters = array_merge( $parameters, array( 'access_token' => $accessToken ) ); - - return $this->request( $this->apiUrl . $request, $parameters, 'DELETE' ); - - } - - } - \ No newline at end of file + namespace Iamstuartwilson; + + /** + * Simple PHP Library for the Strava v3 API + * + * @author Stuart Wilson + * + * @link https://github.com/iamstuartwilson/strava + */ + + class StravaApi + { + const BASE_URL = 'https://www.strava.com/'; + + public $lastRequest; + public $lastRequestData; + public $lastRequestInfo; + + protected $apiUrl; + protected $authUrl; + protected $clientId; + protected $clientSecret; + + private $accessToken; + + /** + * Sets up the class with the $clientId and $clientSecret + * + * @param int $clientId + * @param string $clientSecret + */ + public function __construct($clientId = 1, $clientSecret = '') + { + $this->clientId = $clientId; + $this->clientSecret = $clientSecret; + $this->apiUrl = self::BASE_URL . 'api/v3/'; + $this->authUrl = self::BASE_URL . 'oauth/'; + } + + /** + * Appends query array onto URL + * + * @param string $url + * @param array $query + * + * @return string + */ + protected function parseGet($url, $query) + { + $append = strpos($url, '?') === false ? '?' : '&'; + + return $url . $append . http_build_query($query); + } + + /** + * Parses JSON as PHP object + * + * @param string $response + * + * @return object + */ + protected function parseResponse($response) + { + return json_decode($response); + } + + /** + * Makes HTTP Request to the API + * + * @param string $url + * @param array $parameters + * + * @return mixed + */ + protected function request($url, $parameters = array(), $request = false) + { + $this->lastRequest = $url; + $this->lastRequestData = $parameters; + + $curl = curl_init($url); + + $curlOptions = array( + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_REFERER => $url, + CURLOPT_RETURNTRANSFER => true, + ); + + if (! empty($parameters) || ! empty($request)) { + if (! empty($request)) { + $curlOptions[ CURLOPT_CUSTOMREQUEST ] = $request; + $parameters = http_build_query($parameters); + } else { + $curlOptions[ CURLOPT_POST ] = true; + } + + $curlOptions[ CURLOPT_POSTFIELDS ] = $parameters; + } + + curl_setopt_array($curl, $curlOptions); + + $response = curl_exec($curl); + $error = curl_error($curl); + + $this->lastRequestInfo = curl_getinfo($curl); + + curl_close($curl); + + if (! $response) { + return $error; + } else { + return $this->parseResponse($response); + } + } + + /** + * Creates authentication URL for your app + * + * @param string $redirect + * @param string $approvalPrompt + * @param string $scope + * @param string $state + * + * @link http://strava.github.io/api/v3/oauth/#get-authorize + * + * @return string + */ + public function authenticationUrl($redirect, $approvalPrompt = 'auto', $scope = null, $state = null) + { + $parameters = array( + 'client_id' => $this->clientId, + 'redirect_uri' => $redirect, + 'response_type' => 'code', + 'approval_prompt' => $approvalPrompt, + 'scope' => $scope, + 'state' => $state, + ); + + return $this->parseGet( + $this->authUrl . 'authorize', + $parameters + ); + } + + /** + * Authenticates token returned from API + * + * @param string $code + * + * @link http://strava.github.io/api/v3/oauth/#post-token + * + * @return string + */ + public function tokenExchange($code) + { + $parameters = array( + 'client_id' => $this->clientId, + 'client_secret' => $this->clientSecret, + 'code' => $code, + ); + + return $this->request( + $this->authUrl . 'token', + $parameters + ); + } + + /** + * Deauthorises application + * + * @link http://strava.github.io/api/v3/oauth/#deauthorize + * + * @return string + */ + public function deauthorize() + { + return $this->request( + $this->authUrl . 'deauthorize', + $this->generateParameters(array()) + ); + } + + /** + * Sets the access token used to authenticate API requests + * + * @param string $token + */ + public function setAccessToken($token) + { + return $this->accessToken = $token; + } + + /** + * Sends GET request to specified API endpoint + * + * @param string $request + * @param array $parameters + * + * @example http://strava.github.io/api/v3/athlete/#koms + * + * @return string + */ + public function get($request, $parameters = array()) + { + $parameters = $this->generateParameters($parameters); + $requestUrl = $this->parseGet($this->apiUrl . $request, $parameters); + + return $this->request($requestUrl); + } + + /** + * Sends PUT request to specified API endpoint + * + * @param string $request + * @param array $parameters + * + * @example http://strava.github.io/api/v3/athlete/#update + * + * @return string + */ + public function put($request, $parameters = array()) + { + return $this->request( + $this->apiUrl . $request, + $this->generateParameters($parameters), + 'PUT' + ); + } + + /** + * Sends POST request to specified API endpoint + * + * @param string $request + * @param array $parameters + * + * @example http://strava.github.io/api/v3/activities/#create + * + * @return string + */ + public function post($request, $parameters = array()) + { + + return $this->request( + $this->apiUrl . $request, + $this->generateParameters($parameters) + ); + } + + /** + * Sends DELETE request to specified API endpoint + * + * @param string $request + * @param array $parameters + * + * @example http://strava.github.io/api/v3/activities/#delete + * + * @return string + */ + public function delete($request, $parameters = array()) + { + return $this->request( + $this->apiUrl . $request, + $this->generateParameters($parameters), + 'DELETE' + ); + } + + /** + * Adds access token to paramters sent to API + * + * @param array $parameters + * + * @return array + */ + protected function generateParameters($parameters) + { + return array_merge( + $parameters, + array( 'access_token' => $this->accessToken ) + ); + } + } diff --git a/composer.json b/composer.json index a0174d0..92a6bac 100644 --- a/composer.json +++ b/composer.json @@ -1,14 +1,13 @@ { "name": "iamstuartwilson/strava", - "description": "Simple PHP Library for the Strava v3 API", - "homepage": "https://github.com/iamstuartwilson/strava", - "autoload": { - "classmap": [ - "StravaApi.php" - ] - }, - "require":{ - "php":">=5.3" - }, - "minimum-stability":"dev" + "description": "PHP implementation of the Strava V3 API", + "license": "MIT", + "authors": [ + { + "name": "Stuart Wilson", + "email": "bonjour@iamstuartwilson.com" + } + ], + "minimum-stability": "dev", + "require": {} }