3rdpartyeve/phealng

此包已废弃,不再维护。未建议替代包。

PHP Eve API 库,一个简单的 PHP 库,用于访问 EVE Online API

2.3.5 2016-10-02 14:56 UTC

README

CCP 最近(2018年)结束了 XML API,因此继续这个库并没有太大的意义。此存储库已被存档,以防您将来需要再次访问代码,但如果您读到此处,那么这个库很可能不是您要找的。

祝好,PP

PhealNG

Latest Stable Version Total Downloads

版权 (C) 2012 - 2018 由 Peter Petermann 所有权利保留。

PhealNG 是对 Pheal 的重构,以满足更现代的 PHP 开发标准,例如支持命名空间和 PSR-0 类加载。

许可证

Pheal 在 MIT 风格的许可证下发布,有关更多信息请参阅 LICENSE.txt。

特性

  • 当 EVE API 发生变化时,不需要更改

要求

  • PHP 5.4+

安装

composer

PhealNG 将通过 composer 的 packagist 以 3rdpartyeve/phealng 包的形式提供,如果您还不了解 composer,现在是学习它的好时机。

您也可以从 github 下载它,但使用 composer 会更容易!

PhealNG 使用方法

先决条件

composer.json

在您的项目根目录创建一个名为 composer.json 的文件,其中包含以下 json 数据

{
    "require": {
        "3rdpartyeve/phealng": "~2.0"
    }
}

注意:将 ~2.0 替换为您想要使用的任何版本,您可以使用 dev-master,但它可能是不稳定的。

提示: PhealNG 遵循语义版本控制 http://semver.org

提示: Latest Stable Version

composer

Composer 是一个旨在管理 PHP 应用程序依赖关系的工具。如果您尚未安装 composer,请在此处查看 composer 安装说明: https://getcomposer.org.cn/doc/00-intro.md#installation-nix

运行 composer

现在,在您项目的根目录中,运行以下命令,该命令将为您下载 PhealNG 并更新 vendor/autoload.php

$ php composer.phar install

最基础的 PhealNG 脚本

这是一个非常基础的示例,它应该可以独立运行(只需要依赖 pheal)。显然,如果你使用了一个框架,某些步骤可能看起来不同,例如,如果你使用 symfony2,你可能已经设置了 composer 并且已经有了所需的自动加载。

PhealNG 通过作用域和调用的方法来构建请求,在这个示例中,使用的作用域是 "server",请求的 API 页面是 ServerStatus.xml.aspx

有关可用的 API 页面的更多信息,请参阅 http://wiki.eve-id.net/APIv2_Page_Index

<?php
require_once 'vendor/autoload.php';

//import namespace
use Pheal\Pheal;

// create pheal object with default values
// so far this will not use caching, and since no key is supplied
// only allow access to the public parts of the EVE API
$pheal = new Pheal();

// requests /server/ServerStatus.xml.aspx
$response = $pheal->serverScope->ServerStatus();

echo sprintf(
  "Hello Visitor! The EVE Online Server is: %s!, current amount of online players: %s",
  $response->serverOpen ? "open" : "closed",
  $response->onlinePlayers
);

稍微复杂一点的脚本

大多数 API 页面需要带有一定权限集的 API 密钥,在这个示例中,我们将请求 char/CharacterSheet.xml.aspx,这需要传递一个字符 ID 作为参数,以及包含访问掩码 8 的 API 密钥。

<?php
require_once 'vendor/autoload.php';

//import namespace
use Pheal\Pheal;
use Pheal\Core\Config;

// the information required by this example, usually, your application would
// prompt your user for this, and/or use its database to read those information
// information like the characterID can be obtained through the EVE API,
// please check the documentation at http://wiki.eve-id.net/APIv2_Page_Index for more information
$keyID = 123456;
$vCode = "AbcDEFghYXZadfADFasdFASDFasdfQWERGHADAQerqEFADSFASDfqQER";
$characterID = 1234567;

// Pheal configuration
// Pheal may be configured through variables at the \Pheal\Cache\FileStorage Singleton object
// this allows to use different fetchers, caches, archives etc.

// setup file cache - CCP wants you to respect their cache timers, meaning
// some of the API Pages will return the same data for a specific while, or worse
// an error. If you use one of the availabe caching implementations,
// pheal will do the caching transparently for you.
// in this example we use the file cache, and configure it so it will write the cache files
// to /tmp/phealcache
Config::getInstance()->cache = new \Pheal\Cache\FileStorage('/tmp/phealcache/');

// The EVE API blocks applications which cause too many errors. Requesting a page
// that the API key does not allow to request is one of those possible errors.
// Pheal can be configured so pheal will request the AccessMask of a specific key
// and block requests to API Pages not covered by that key.
Config::getInstance()->access = new \Pheal\Access\StaticCheck();

// create pheal object with default values
// so far this will not use caching, and since no key is supplied
// only allow access to the public parts of the EVE API
//
// in this example, instead of using the scopenameScope getter,
// we set the scope directly in the constructor
$pheal = new Pheal($keyID, $vCode, "char");

try {
    // parameters for the request, like a characterID can be added
    // by handing the method an array of those parameters as argument
    $response = $pheal->CharacterSheet(array("characterID" => $characterID));

    echo sprintf(
        "Hello Visitor, Character %s was created at %s is of the %s race and belongs to the corporation %s",
        $response->name,
        $response->DoB,
        $response->race,
        $response->corporationName
    );

// there is a variety of things that can go wrong, like the EVE API not responding,
// the key being invalid, the key not having the rights to call the method
// or the characterID being wrong - just to name a few. So it is basically
// a good idea to catch Exceptions. Usually you would want to log that the
// exception happend and then decide how to inform the user about it.
// In this example we simply catch all PhealExceptions and display their message
} catch (\Pheal\Exceptions\PhealException $e) {
    echo sprintf(
        "an exception was caught! Type: %s Message: %s",
        get_class($e),
        $e->getMessage()
    );
}

配置选项

在上一个示例中已经介绍了两个配置选项,但是还有更多。有关更多信息,建议阅读 vendor/3rdpartyeve/phealng/lib/Pheal/Core/Config.php 文件的内容。

缓存

CCP 希望你尊重他们的缓存计时器,这意味着一些 API 页面将返回相同的数据一段时间,或者更糟,返回错误。如果你使用可用的缓存实现之一,pheal 将为你透明地执行缓存。Pheal 默认提供这些实现

  • NullStorage(无缓存!)
  • FileStorage
  • ForcedFileStorage
  • HashedNameFileStorage
  • MemcacheStorage
  • MemcachedStorage
  • RedisStorage
  • PdoStorage(数据库缓存)

请参阅类 api 文档获取更多信息。

PdoStorage(数据库缓存)

为了在数据库中缓存请求,你必须首先创建表。

对于 MySQL 数据库,你可以使用以下示例片段

    CREATE TABLE `phealng-cache` (
        `userId` INT(10) UNSIGNED NOT NULL,
        `scope` VARCHAR(50) NOT NULL,
        `name` VARCHAR(100) NOT NULL,
        `args` VARCHAR(250) NOT NULL,
        `cachedUntil` TIMESTAMP NOT NULL,
        `xml` LONGTEXT NOT NULL,
        PRIMARY KEY (`userId`, `scope`, `name`, `args`)
    )
    COMMENT='Caching for PhealNG'
    COLLATE='utf8_general_ci'
    ENGINE=InnoDB;

日志记录器

Pheal 包含 3 个日志记录器可以使用,默认的一个是 Null Logger \Pheal\Log\NullStorage,它不会记录任何内容。然后是 Legacy Logger \Pheal\Log\FileStorage,它可以将日志记录到文件中(更多信息请参阅其代码)。请注意,该功能已被弃用。然后是 PsrLogger,它基本上是一个可以围绕任何现有的 PSR-3 兼容日志记录器包装的类,因此 Pheal 可以使用你的框架的日志记录器输出其日志信息。

使用示例

    <?php
    require_once 'vendor/autoload.php';
    
    // initialize a PSR-3 compatible logger. in this example, we assume that you have added monolog/monolog to your 
    // composer dependencies, but really this part of the code depends on which PSR-3 compatible logger you use,
    // and where you get it from usually depends on your framework.
    $psr = new \Monolog\Logger('test');
    $psr->pushHandler(new \Monolog\Handler\StreamHandler('test.log', Logger::DEBUG));
    
    // configure pheal to use the \Pheal\Log\PsrLogger, handing over the PSR-3 compatible logger instance to the new object
    \Pheal\Core\Config::getInstance()->log = new \Pheal\Log\PsrLogger($psr);
    
    // any call to the CCP api will now be logged, for example:
    $pheal = new \Pheal\Pheal();
    $pheal->serverScope->ServerStatus();
    
    // should cause a log entry like: 
    // [2014-12-29 13:30:04] test.INFO: GET to https://api.eveonline.com/server/ServerStatus.xml.aspx (0.0802s) [] []

问题 / 错误

如果你发现 PhealNG 有任何问题,请使用 githubs 的问题跟踪器 https://github.com/3rdpartyeve/phealng/issues

待办事项

  • 更多文档

链接

联系方式

贡献者

  • Daniel Hoffend (Wollari)

致谢

  • PhaelNG 基于已弃用的 Phael
  • PhaelNG 使用 PHP 编写
  • Phael 基于 EAAL
  • Phael 是为使用 EVE Online API 而构建的