Coder Social home page Coder Social logo

fitbit-http-php's Introduction

fitbit-http-php tag Build Status codecov StyleCI

Fitbit PHP SDK

Disclaimer

This project is not maintained anymore and therefore it will be archived. I'm abandoning it because I don't own any Fitbit product anymore so I can't do testing, and as far as I know it is not being used by anyone else.

If you are interested in this project feel free to contact me.

About

This is a package for acessing the Fitbit official Web API from your PHP language project. It is designed so you can easily query and retrieve all data hold on their platform from Activity Logs to Sleep or Heart Rate information.

Installation

Require this package, with Composer, in the root directory of your project.

$ composer require namelivia/fitbit-http-php:~0.0.1

Getting started

Before getting started retrieving date from the Fitbit Web API you first need to register your application on their platfrom dev.fitbit.com. Once you submit the form and your application has ben registred, they will provide you with some credentials you need on the next step. These credentials are:

  • OAuth 2.0 Client ID
  • Client Secret Also you will provide a callback URL is the URL the client will be redirected from the Fitbit Platfrom after authorizing your application to access their data.

Proving credentials

Now to get started you need to create an new instance of the API like this, note that four strings parameters are required to do so:

$api = new \Namelivia\Fitbit\Api\Api(
	'someClientId', #clientId
	'someClientSecret', #clientSecret
	'https://myapp.com/authorized', #redirectUrl
	'/tmp/token' #tokenPath
);

The first paramter is the OAuth 2.0 Client ID you got from the Fitbit platform, the second parameter is the Client Secret you got from the Fitbit platform too. The third parameter is your applications URL where the users are going to be directed to from the Fitbit authorization screen. And the fourth one is the path where the authorization tokens is going to be stored in order to be rememberd so the authorization screen does not happens everytime.

  • clientId: Your application client Id.
  • clientSecret: Your application client secret.
  • redirectUrl: Your application redirect URL.
  • tokenPath: Your token path.

Authorizing

Now that you have the api, you need to check if the application is alredy authorized by calling isAuthorized, if you have a valid token stored on the token path you provided before or you have already providen an authorization code the following won't be necessary. If the api is still unauthorized an authorization code needs to be requested from the authorization Url, this Url can be retrieved by calling the getAuthUri function. You can redirect your user to that external Uri and after Fitbit will redirect the user back to your app by making use of the return url provided with the code as a parameter. In this example the code from the Uri is manually typed by the user. Finally once the you have the authorization code the api must be providen with it by calling the setAuthorizationCode function.

if (!$api->isAuthorized()) {
	echo 'Go to: ' . $api->getAuthUri() . "\n";
	echo "Enter verification code: \n";
	$code = trim(fgets(STDIN, 1024));
	$api->setAuthorizationCode($code);
}

Initializing

Finally the last step before making the actual requests is to initialize the api, you can check if the api is already initialized by calling isInitialized and initialize it by calling initialize. This will refresh or create tokens if needed and store them in the token path providen before. Now the api is ready to query data.

if (!$api->isInitialized()) {
	$api->initialize();
}

Getting a fitbit instance

For querying the data you need to get a fitbit instance, and for doing so you need to pass it the recently created an initialized api to the constructor like this:

$fitbit = new \Namelivia\Fitbit\Api\Fitbit($api);

Retrieving data

Everything is ready, you can start asking for the data.

$fitbit->activities()->favorites()->get();

For further information refer to the documentation linked in the next section.

Documentation

The full documentation can be found in the wiki section of the github repository. Also you can refer to the official Fitbit Web API documentation

License

MIT

Contributing

Any suggestion, bug reports, prs or any other kind enhacements are welcome. Just open an issue first, for creating a PR remember this project has linting checkings and unit tests so any PR should comply with both before beign merged, this checks will be automatically applied when opening or modifying the PR's.

Local development

This project comes with a docker-compose.yml file so if you use Docker and docker-compose you can develop without installing anything on your local environment. Just run docker-compose up --build for the first time to setup the container and launch the tests. PHPUnit is configured as the entrypoint so just run docker-compose up everytime you want the tests to execute on the Dockerized PHP development container.

fitbit-http-php's People

Contributors

dependabot[bot] avatar dependabot-preview[bot] avatar purposebuiltscott avatar namelivia avatar olindn avatar tomaspilar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.