mab05k/oanda-client

Oanda v20 REST-API 的 PHP 客户端

v0.5.0 2023-06-03 23:09 UTC

README

这是一个用于与 Oanda REST-V20 API 交互的 PHP 客户端库。该 API 考虑了灵活性,并支持使用多个 Oanda 账户。

贡献

如果您想帮助贡献,请访问贡献文档。

查看变更日志了解最近的更改。

端点

配置

配置使用标准 Symfony Bundle 配置完成

# config/bundles.php

<?php

return [
    Mab05k\OandaClient\Bridge\Symfony\Bundle\BosOandaClientBundle::class => ['all' => true],
    Http\HttplugBundle\HttplugBundle::class => ['all' => true],
    JMS\SerializerBundle\JMSSerializerBundle::class => ['all' => true],
];
# config/packages/oanda_client.yaml

mab05k_oanda_client:
  hostname: https://api-fxpractice.oanda.com
  stream_hostname: https://stream-fxpractice.oanda.com
  path_prefix: /v3
  accounts:
  - name: primary_account
    account_id: 000-000-0000000-000
    account_secret: my_account_secret
  - name: secondary_account
    account_id: 000-000-0000000-001
    account_secret: my_account_secret

前置扩展

当包由 Symfony 加载时,它将自动为您创建以下Httplug 配置。您可以根据自己的配置覆盖或扩展此配置。

# This is an example, and does not need to be manually added to your configuration files
httplug:
  plugins:
    logger: 
      logger: 'logger'
  clients:
    mab05k_oanda_client:
      factory: 'httplug.collector.factory.guzzle6'
      config:
        timeout: 3
       plugins:
        - add_host:
            host: '%mab05k_oanda_client.hostname%'
            replace: true
        - add_path:
            path: '%mab05k_oanda_client.path_prefix%'
        - header_defaults:
            headers:
              Content-Type: 'application/json'

我们还为 JMS Serializer 设置了默认的 DateTime 格式,以匹配 Oanda 的 DateTime 格式

# This is an example, and does not need to be manually added to your configuration files
jms_serializer:
  handlers:
    datetime:
      default_format: 'Y-m-d\TH:i:s.u???\Z'

身份验证

Oanda 客户端包根据您的配置处理发送授权头。默认情况下,它将使用配置中的第一个账户。如果您使用多个账户,您必须遵循多个账户部分中的文档。

使用

使用依赖注入将客户端注入到您的服务中

<?php

namespace App\Service;

use Mab05k\OandaClient\Client\AccountClient;

class OandaAccountService
{
    /**
     * @var AccountClient
     */
    private $accountClient;
    
    public function __construct(AccountClient $accountClient) 
    {
        $this->accountClient = $accountClient;
    }
    
    public function doSomethingWithAccount()
    {
        $account = $this->accountClient->account();
        // do something and return the result...
    }
}

客户端