Hostinger API SDK 详解:PHP、Python 与 TypeScript 开发指南
Hostinger 提供官方 SDK,以简化与我们 API 的交互,让开发者更轻松、更快速地将 Hostinger 服务集成到自己的应用中。目前,我们提供以下语言的 SDK:
快速开始
PHP SDK
要通过 Composer 安装绑定库,请将以下内容添加到 composer.json:
{ "repositories": [ { "type": "vcs", "url": "https://github.com/hostinger/api-php-sdk.git" } ], "require": { "hostinger/api-php-sdk": "*" }}然后运行 composer install。
使用示例:
<?phprequire_once(__DIR__ . '/vendor/autoload.php');
// Configure Bearer authorization: apiToken$config = Hostinger\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');
$apiInstance = new Hostinger\Api\BillingCatalogApi( // If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`. // This is optional, `GuzzleHttp\Client` will be used as default. client: new GuzzleHttp\Client(), config: $config);
try { $result = $apiInstance->getCatalogItemListV1(); print_r($result);} catch (Exception $e) { echo 'Exception when calling BillingCatalogApi->getCatalogItemListV1: ', $e->getMessage(), PHP_EOL;}Python SDK
通过 pip 安装:
pip install hostinger_api使用示例:
import hostinger_apifrom hostinger_api.rest import ApiExceptionfrom pprint import pprint
# Configure Bearer authorization: apiTokenconfiguration = hostinger_api.Configuration( access_token = os.environ["BEARER_TOKEN"])
# Enter a context with an instance of the API clientwith hostinger_api.ApiClient(configuration) as api_client: # Create an instance of the API class api_instance = hostinger_api.BillingCatalogApi(api_client)
try: # Get catalog item list api_response = api_instance.get_catalog_item_list_v1() print("The response of BillingCatalogApi->get_catalog_item_list_v1:\n") pprint(api_response) except ApiException as e: print("Exception when calling BillingCatalogApi->get_catalog_item_list_v1: %s\n" % e)TypeScript SDK
通过 npm 安装:
npm install hostinger-api-sdk@0.0.4 --save使用示例:
import { Client } from 'hostinger-api-sdk';
const client = new Client('your-api-token');
client.get('/v1/user').then(response => { console.log(response);});身份验证
所有 SDK 都需要 API 令牌(API token)来对你的请求进行身份验证。你可以在 Hostinger 控制面板的账户设置中的 API 页面获取你的 API 令牌。
查看上方链接的各 GitHub 仓库,探索我们 SDK 的各项功能与高级特性。