jyokum / healthgraph

基于Guzzle构建的RunKeeper Health Graph API的PHP封装器

0.3.0 2014-01-04 16:00 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:09:38 UTC


README

Latest Stable Version Total Downloads Build Status

健康图客户端

健康图客户端是Health Graph API的PHP封装器

通过Composer安装

在您的composer.json中添加HealthGraph

{
    "require": {
        "jyokum/healthgraph": "*"
    }
}

下载库

$ php composer.phar update

安装后,您需要在代码中的某个位置要求Composer的自动加载器

require_once 'vendor/autoload.php';

生成连接链接/按钮

require_once 'vendor/autoload.php';
use HealthGraph\Authorization;

$client_id = 'xxxxxxxxxxxxxxxxxxxxx';
$redirect_url = 'http://example.com/authorize';

// Link
$link = Authorization::getAuthorizationLink($client_id, $redirect_url);
echo "<a href='$link'>Connect</a>";

// Button
$button = Authorization::getAuthorizationButton($client_id, $redirect_url);
echo $button['html'];

从授权代码获取令牌

require_once 'vendor/autoload.php';
use HealthGraph\Authorization;

$client_id = 'xxxxxxxxxxxxxxxxxxxxx';
$client_secret = 'xxxxxxxxxxxxxxxxxxxxx';
$redirect_url = 'http://example.com/authorize';

if (isset($_REQUEST['code'])) {
    $token = Authorization::authorize($_GET['code'], $client_id, $client_secret, $redirect_url);
    some_function_to_save_the_token($token);
}

撤销令牌

require_once 'vendor/autoload.php';
use HealthGraph\Authorization;

$token = some_function_to_retrieve_the_token();
Authorization::deauthorize($token->access_token);

检索用户数据

require_once 'vendor/autoload.php';
use HealthGraph\HealthGraphClient;

$token = some_function_to_retrieve_the_token();

$hgc = HealthGraphClient::factory();
$hgc->getUser(array(
    'access_token' => $token['access_token'],
    'token_type' => $token['token_type'],
));

// get profile
$hgc->GetProfile();

// get settings
$hgc->GetSettings();

// get fitness activity list
$activities = $hgc->FitnessActivityFeed()->getAll();

更新资料

$hgc = HealthGraphClient::factory();
$hgc->getUser(array(
    'access_token' => $token['access_token'],
    'token_type' => $token['token_type'],
));
$data = array('athlete_type' => 'Athlete');
$hgc->UpdateProfile($data);

获取健身活动

$hgc = HealthGraphClient::factory();
$hgc->getUser(array(
    'access_token' => $token['access_token'],
    'token_type' => $token['token_type'],
));

// Get the entire feed, this could return a lot of data.
$command = $hgc->getIterator('GetFitnessActivityFeed');
$result = $command->toArray();

// Get the 5 most recent activities.
$command = $hgc->getIterator('GetFitnessActivityFeed')->setLimit(5);
$result = $command->toArray();

添加新的健身活动

$hgc = HealthGraphClient::factory();
$hgc->getUser(array(
    'access_token' => $token['access_token'],
    'token_type' => $token['token_type'],
));
$command = $hgc->getCommand('NewFitnessActivity', array(
    "type" => "Running",
    "start_time" => "Sat, 1 Jan 2011 00:00:00",
    "duration" => 600,
    "total_distance" => 1000,
    "notes" => "Ran 1000 meters in 600 seconds"
));
$result = $command->execute();