tubehosting/tubephp-api

为 tube-hosting.com REST API 提供的简单 PHP API 客户端

v1.2.1 2023-01-24 16:18 UTC

This package is auto-updated.

Last update: 2024-09-22 20:09:25 UTC


README

说明

这个 PHP 库是 tube-hosting.com API 的简单 API 包装器/客户端。
它基于提供的 文档
包装器是针对所谓的“模式”构建的,其中指定的端点被分配到,按照特定的顺序。
在文档中,我们看到端点被分为十三种标签,每种标签都放入一个对象中。
它们的顺序如下

请参阅 Documentation.md

安装

Tube-Hosting PHP API 通过 Packagist 提供,建议通过 Composer 在您的项目中安装。
在您的项目中安装 Composer 后,只需将以下行添加到您的 composer.json 文件中。

"tubehosting/tubephp-api": "^1.2.1"

或者

$ composer require tubehosting/tubephp-api

在您的 shell 中运行

此库还需要 PHP 版本 7.1 或更高版本以及 PHP cURL 扩展。

用法

这里有一个相当简单的示例,我们登录到 Tube-Hosting 账户,获取 VPS 的信息并显示这些信息

一个简单的示例

<?php
use TubeAPI\Objects; 
use TubeAPI\Exceptions;

require 'vendor/autoload.php'; //Load the Composer autoloader

$password = "Password123";   //you can use password authentication, but this is not recommended, please use the api key instead (scroll down for more information about this) 
$mail = "E-Mail@Address.tld"; 

try {
    //login using the credentials of an existing tube-hosting.de account (the login returns a new JWTTokenResponse)
    $user = Objects\User::login(new Objects\AuthenticationLoginData($mail, $password)); 
    
    $vps = Objects\VPS::getServerById(488); //get a VPS by the id, returns new VPS object
    $vpsStatus = Objects\VPS::getServerStatusById(488); //get status information of VPS, returns new VpsStatus Object

    //print different information about the VPS, provided in the VPS and VpsStatus Object 
    print "Overview ".$vps->getVpsType()." - ".$vps->getName() . "\n"; 
    print "Node: " . $vps->getNodeId() . "\n"; 
    print "IP: " . $vps->getPrimaryIPv4()->getIpv4()->getIpv4() ."\n"; 
    print "OS: " . $vps->getOsDisplayName() . "\n"; 
    print " - " . $vps->getCoreCount() . " CPU Cores, Usage: ".(int)($vpsStatus->getCpu()*100) . "%\n"; 
    print " - " . number_format($vpsStatus->getMem() / 1048576) . "/".  number_format($vps->getMemory()) ." GB RAM\n"; 
    print " - " . $vps->getDiskType() ." -> " . number_format($vps->getDiskSpace()/1024) ." GB\n"; 
    print "Price: €" . $vps->getPrice()/100 . " (".$vps->getPriceType().")\n"; 
    print "Bought on: " . $vps->getStartDate() . "\n";
    print "Paid until: " . $vps->getRuntime() . "\n";

}catch (Exceptions\RequestException $e) {
    print $e->getMessage() . "\n";
    //you can also get more detailed information (like http status code, response, curl_getInfo, etc.)
    //for example with the http status code: 
    print "Status code: " . $e->getHttpStatusCode() . "\n";
}

(示例) 输出

Overview KVM - server488
Node: 6
IP: 193.111.248.90
OS: Debian 11 (Bullseye) 64-bit, German
 - 2 CPU Cores, Usage: 32%
 - 1,165/2,048 GB RAM
 - SSD -> 24 GB
Price: €5 (GROSS)
Bought on: 2021-11-30T10:09:22Z
Paid until: 2024-02-26T00:00:00Z

请查看更多示例 在示例目录中

使用 API 令牌登录

除了您的电子邮件和密码

$password = "Password123"; 
$mail = "E-Mail@Address.tld"; 

您还可以使用您的 API 令牌。

TubeAPI\TubeAPI::$token = "yourtoken"; //set the api token

请确保也删除登录行($user = Objects\User::login(new Objects\AuthenticationLoginData($mail, $password));),因为这会覆盖 API 密钥。

示例实现可以在 示例目录中找到

授权协议

本软件根据 MIT 许可协议 分发。