gbv/jskos-http

此包已 废弃 并不再维护。没有建议的替代包。

JSKOS API 实现(服务器和客户端)

0.3.1 2017-10-18 08:30 UTC

This package is auto-updated.

Last update: 2024-09-11 17:54:01 UTC


README

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

要求

需要 PHP 7.0 或 PHP 7.1,包 jskos,以及任何列出的 php-http/client-implementation 包。

错误和功能请求在 GitHub 上跟踪。

安装

使用 composer

安装一个 php-http/client-implementation 和此包的最新版本,例如。

composer require php-http/curl-client gbv/jskos-http

这将为您的项目自动创建 composer.json(除非它已经存在)并将 jskos-http 作为依赖项添加。Composer 还生成 vendor/autoload.php 以自动加载所有依赖项。

使用方法和示例

请参阅 examples 目录中的示例脚本和 jskos-php-examples 目录中的示例应用程序。

客户端

请参阅 Client 类以查询 JSKOS API 并返回 Result 对象。

use JSKOS\Client;

$client = new Client('http://example.org/');
$result = $client->query(['uri'=>$uri]);

if (count($result)) {
  ...
}

可以传递一个可选的 Http\Client\HttpClient 作为第二个参数。例如,使用此参数记录所有 HTTP 请求。

$handler = \GuzzleHttp\HandlerStack::create();
foreach(['{method} {uri}', '{code} - {res_body}'] as $format) {
	$handler->unshift(
		\GuzzleHttp\Middleware::log(
			$logger, // e.g. Monolog\Logger
			new \GuzzleHttp\MessageFormatter($format)
		)
	);
}

$httpClient = \Http\Adapter\Guzzle6\Client::createWithConfig([
    'handler' => $handler,
]);

$jskoClient = new Client('http://example.org/', $httpClient);

服务器

Server 类使用 PSR-7 HTTP 消息接口 包装一个 JSKOS Service,因此它可以与您喜欢的 PSR-7 框架一起使用。以下是一个使用 Slim 的示例。

$server = new JSKOS\Server($service);

$app = new Slim\App();

$app->get('/api', function ($request) use ($server) {
    return $server->query($request);
});

$app->run();

但是,无需使用额外的框架来支持简单的 HTTP GET 请求。

use JSKOS;

$server = new Server($service);
$response = $server->queryService($_GET, $_SERVER['PATH_INFO'] ?? '');
Server::sendResponse($response);

作者和许可证

Jakob Voß jakob.voss@gbv.de

JSKOS-HTTP 在 LGPL 许可下发布 - 详细信息请参阅 LICENSE.md

另请参阅

JSKOS 是作为项目 coli-conc 的一部分创建的:https://coli-conc.gbv.de/

JSKOS 的当前规范可在 http://gbv.github.io/jskos/ 查找。