定制 georgo/mojio 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

georgo/mojio

Composer 安装命令:

composer require georgo/mojio

包简介

PHP client to login and access the Mojio API

README 文档

README

As PHP is the language of choice for many of you developers out there, we have put together a simplified client built on top of Guzzle to help you get started. This client is still very much in it's alpha stages, we appologize for any bugs and incomplete features.

Installation

Using Composer (recommended)

The client has been added to packagist under the name georgo/mojio and can be included in your project using Composer.

  1. First you will need to add "georgo/mojio" as a dependency in your composer.json file (currently only the dev-master is available, more stable versioning coming soon):

    {
        "require": {
          "georgo/mojio": "~2.0"
        }
    }
  2. Next you will need to download an install Composer and dependancies:

    curl -sS https://getcomposer.org/installer | php
    php composer.phar install
  3. Lastly, you need to include the Composer autoloader in your bootstrap:

    require '[/path/to/vendor]/autoload.php';

From Source (GitHub)

If you do not want to use Composer, you can download or checkout the complete source off github. You must also download or checkout Guzzle

Getting Started

To begin developing with Mojio PHP client, you will need your very own application ID and secret key. First you will need to create an account and login to the MOJIO developer center.

Once you have logged in, you can create a new Application. From here, you will want to copy the Application ID and the Secret Key, these will be required to initialize the MOJIO client

Initializing the Client

To get started using the client, instantiate a new instance of the MOJIO client class. This is where you will need to pass in the Application ID and Secret Key, as well as the developer environment you are using (Sandbox, or Live).

use Mojio\Api\Client;

require '[path/to/vendor]/autoload.php';

$appId = "{APPID}";
$secretKey = "{SecretKey}";

$client = Client::factory([
        'base_url' => Client::LIVE,
        'app_id' => $appId,
        'secret_key' => $secretKey
]);

// ...

Authenticate a Mojio User

Now that your MojioClient is associated with your app, you can get started making some API calls. However, many of Mojio API calls also require an authorized user to be associated with the client session. A user can grant you access using Mojio OAuth2 service, and client calls. For redirectUri, https scheme is required.

// ...

// Set the redirect URI to point to this exact same script.
$redirectUri = 'https://' . $_SERVER['HTTP_HOST'] 
                   . strtok($_SERVER['REQUEST_URI'], '?');
                   
if(!isset($_GET['code'])) {
    // Initiate an OAuth request
    header('Location: '.$client->getAuthorizationUrl($redirectUri));
    exit;
} else {
    // Handle the OAuth response
    $client->authorize($redirectUri, $_GET['code']);
}

// ...
// Logout user.
$client->logout();

Please note, you must add the $redirectUri to the allowed Redirect URIs in your application settings on the Developer Center.

Fetching Data

To retrieve a set of a particular MOJIO entities, you can use the "GET" method. The returned results will depend on what user and application your client session is authorized as. Lists of data will be returned in a paginated form. You are able to set the page size and request a particular page. In order to keep response times fast, it is recommended to keep the page size low.

// ...
// Fetch first page of 15 users
$results = $client->getTrips([
    'pageSize' => 15,
    'page' => 1
]);

foreach( $results as $trip )
{
    // Do something with each trip
    // ...
}

Fetch a specific Entity

By passing in the ID of an entity (often a GUID), you can request a single MOJIO entity from Mojio API.

// ...
$mojioId = "0a5123a0-7e70-12d1-a5k6-28db18c10200";
	
// Fetch mojio from API
$mojio = $client->getMojio([
    "id" => $mojioId
]);
	
// Do something with the mojio data
// ...

Update an Entity

If you want to update and save an entity, you need to first load the entity from the API, make your changes, and then post it back. Typically only the owner of an entity will be authorized to save changes and not all properties of an entity will be editable (for example, for an App, only the Name and Description properties can be changed).

// ...
$appId = "0a5123a0-7e70-12d1-a5k6-28db18c10200";
	
// Fetch app from API
$app = $client->getApp([
    'id' => $appId
]);
	
// Make a change
$app->Name = "New Application Name";
	
// Save the changes
$client->saveEntity([
    'entity' => $app
]);

Get a list of child entities

If you want to fetch all the entities associated with another entity, you can call the GetBy method. For example, if you want to fetch all the events associated with a particular MOJIO device.

use Mojio\Api\Model\MojioEntity;
use Mojio\Api\Model\EventEntity;

    // ...
    $mojioId = "0a5123a0-7e70-12d1-a5k6-28db18c10200";
	
    // Fetch mojio's events
    $events = $client->getList([
        "type" => MojioEntity::getType(),
        "id" => $mojioId,
        "action" => EventEntity::getType()
    ]);
	
    // Or, alternatively
    $mojio = $client->getMojio(['id' => $mojioId]);
    $events = $client->getList([
        'entity' => $mojio,
        'action' => EventEntity::getType()
    ]);

    // ...

Using the Mojio Storage

With the MOJIO API, you are able to store your own private data on Mojio servers as key value pairs. These key value pairs will only be accessible by your application, and you can associate them with any MOJIO entities (ex: MOJIO Device, Application, User, Trip, Event, Invoice, Product).

use Mojio\Api\Model\UserEntity;

    // ...
    $userId = "0a5453a0-7e70-16d1-a2w6-28dl98c10200";  // Some user's ID
    $key = "EyeColour";	// Key to store
    $value = "Brown"; 	// Value to store

    // Save user's eye colour
    $client.setStored([
        'type' => UserEntity::getType(),
        'id' => $userId,
        'key' => $key
        'value' => $value
    ]);

    // ...
    // Retrieve user's eye colour
    $client.getStored([
        'type' => UserEntity::getType(),
        'id' => $userId,
        'key' => $key
    ]);

Requesting Event Updates

Instead of continuously polling the API to check for updates, you can request Mojio API send a POST request to an endpoint of your choosing everytime certain events are received.

    $mojioId = "0a5123a0-7e70-12d1-a5k6-28db18c10200";

    $sub = SubscriptionEntity::factory(
              'IgnitionOn', // Event Type to receive
              'Mojio',      // Subscription Type
              $mojioId,     // Entity ID
              "https://my-domain-example.com/receiver.php" // Location to send events
    );

    $client->newEntity(['entity' =>$sub]);

And in your "receiver.php" file you will want to process any incoming events:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  $raw = file_get_contents('php://input');
  $event = json_decode( $raw );
  
  // ... Do something with the event!
}
?>

georgo/mojio 适用场景与选型建议

georgo/mojio 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 09 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api」 「client」 「Guzzle」 「mojio」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 georgo/mojio 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 georgo/mojio 我们能提供哪些服务?
定制开发 / 二次开发

基于 georgo/mojio 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 29
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2017-09-23