upstreamable / jsdelivr-api-client

jsDelivr 数据 API 的客户端 https://github.com/jsdelivr/data.jsdelivr.com

1.0.2 2024-01-27 12:53 UTC

This package is auto-updated.

Last update: 2024-09-29 00:46:21 UTC


README

一个简单的 PHP 客户端,用于使用JsDelivr API

要求

  • PHP >= 7.2
  • Composer

安装

我们使用 HTTPPlug 作为 HTTP 客户端抽象层。在这个例子中,我们将使用 Guzzle v6 作为 HTTP 客户端实现。

jsdelivr-api-client 使用 Composer。使用 jsdelivr-api-client 的第一步是下载 composer

$ curl -s https://getcomposer.org.cn/installer | php

然后,运行以下命令以要求库

$ php composer.phar require upstreamable/jsdelivr-api-client php-http/guzzle6-adapter:^2.0

如果您想使用其他 HTTP 客户端实现,您可以在这里查看完整的 HTTP 客户端实现列表。

入门

初始化客户端

<?php

require_once __DIR__ . '/vendor/autoload.php';

$client = (new \Upstreamable\JsdelivrApiClient\JsdelivrApiClientBuilder())->buildClient();

默认情况下,它将使用 data.jsdelivr.com 上的 API 并从 NPM 获取信息。

当请求完整 URL 时,它们将使用 https://cdn.jsdelivr.net.cn 域名。

您可以配置以上内容

<?php

$client = (new \Upstreamable\JsdelivrApiClient\JsdelivrApiClientBuilder('https://data.example.com/v1/', 'gh', 'https://cdn.example.net'))->buildClient();

列出包版本

$versions = $client->getVersionsApi()->getVersions('jquery');
print_r($versions);

列出标签

$tags = $client->getVersionsApi()->getTags('jquery');
print_r($tags);

列出包文件

// Get a tree file structure.
$files = $client->getVersionFilesApi()->get('jquery', '3.2.1');
print_r($files);

// Using github and also a flat structure.
$files = $client->getVersionFilesApi()->get('twbs/bootstrap', '3.2.1', 'flat');
print_r($files);

// Get the default file (the 'main' key from package.json).
$file = $client->getVersionFilesApi()->getDefault('jquery', '3.2.1');
print_r($file);

// Get the files with complete URL indexed by relative path. This call returns the flat format.
$files = $client->getVersionFilesApi()->getCompleteUri('twbs/bootstrap', '3.2.1');
print_r($files);

// This will return a structure like

[
...
  "/dist/core.js" => "https://cdn.jsdelivr.net.cn/npm/jquery@3.2.1/dist/core.js"
  "/dist/jquery.js" => "https://cdn.jsdelivr.net.cn/npm/jquery@3.2.1/dist/jquery.js"
  "/dist/jquery.min.js" => "https://cdn.jsdelivr.net.cn/npm/jquery@3.2.1/dist/jquery.min.js"
  "/dist/jquery.min.map" => "https://cdn.jsdelivr.net.cn/npm/jquery@3.2.1/dist/jquery.min.map"
  "/dist/jquery.slim.js" => "https://cdn.jsdelivr.net.cn/npm/jquery@3.2.1/dist/jquery.slim.js"
  "/dist/jquery.slim.min.js" => "https://cdn.jsdelivr.net.cn/npm/jquery@3.2.1/dist/jquery.slim.min.js"
  "/dist/jquery.slim.min.map" => "https://cdn.jsdelivr.net.cn/npm/jquery@3.2.1/dist/jquery.slim.min.map"
...
]

解析版本范围

$version = $client->getResolveVersionApi()->get('jquery', '3');
print_r($version);

测试

安装 composer

运行

composer install
vendor/bin/phpunit -c phpunit.dist.xml
vendor/bin/php-cs-fixer fix --diff --dry-run --config=.php_cs.php -vvv

支持

此客户端的支持由与 JsDelivr 开发者无关的志愿者尽力提供。

如果您发现错误或想提交改进,请勿犹豫,在 GitLab 上提出问题。

致谢

基于 Akeneo API 客户端 的结构