mrbig/ocpi-protocol
Composer 安装命令:
composer require mrbig/ocpi-protocol
包简介
Library to handle OCPI. Compatible with PSRs. Uses JsonSchema to validate payloads.
README 文档
README
Library to handle OCPI. Compatible with PSRs.
DISCLAIMER
This fork is of chargemap/ocpi-protocol upgraded to OCPI V2.2.1.
This is by no means feature complete, and maybe never will. Currently the most common modules for eMSP and CPO servers are both available, and many of the client classes.
If you miss some classes then please feel free to add them or contact me.
Functionality
Library provides OCPI request/response classes for eMSP interfaces, models, factories and errors. Listing requests/responses are also supported for GET routes. The responses need a corresponding request to be constructed. It is required to ensure the presence and validity of offset and limit request headers and X-Total-Count, X-Limit and Link response headers. So it is quite easy to construct valid listing response or get the next request.
eMSP interface
Respond to CPO
use Chargemap\OCPI\Versions\V2_1_1\Server\Emsp\Sessions\Put\OcpiEmspSessionPutRequest; use Chargemap\OCPI\Versions\V2_1_1\Server\Emsp\Sessions\Put\OcpiEmspSessionPutResponse; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; /** @var RequestInterface $httpRequest */ $sessionPutRequest = new OcpiEmspSessionPutRequest($httpRequest, 'NL', 'TNM', '101'); $session = $sessionPutRequest->getSession(); // Some code... $sessionPutResponse = new OcpiEmspSessionPutResponse($session); /** @var ResponseInterface $response */ $response = $sessionPutResponse->getResponseInterface();
Each request and response class correspond to an eMSP interface route. Request classes must be instantiated by providing PSR-7 compatible request (got from CPO). Internally, it extracts the authorization token from headers, and validates the body (if necessary) against json schema. Then, it constructs corresponding model class using the factories. The model is accessible via a getter.
Response classes must be instantiated with a model instance, even if it's not used (e.g. in Post/Put/Patch responses). It can be converted to PSR-7 compatible response instance using getResponseInterface method.
Use of listing request/response
use Chargemap\OCPI\Versions\V2_1_1\Server\Emsp\Tokens\Get\OcpiEmspTokenGetRequest; use Chargemap\OCPI\Versions\V2_1_1\Server\Emsp\Tokens\Get\OcpiEmspTokenGetResponse; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; /** @var RequestInterface $httpRequest */ $tokenGetRequest = new OcpiEmspTokenGetRequest($httpRequest); $tokens = []; $tokenCount = 0; //Fetch tokens from database... $tokenGetResponse = new OcpiEmspTokenGetResponse($tokenGetRequest, $tokenCount, count($tokens)); foreach ($tokens as $token) { $tokenGetResponse->addToken($token); } // X-Total-Count, X-Limit and Link headers are already set in $response /** @var ResponseInterface $response */ $response = $tokenGetResponse->getResponseInterface();
Request the CPO
This part provides an API SDK to request the CPO. To use it, you need to instantiate the OcpiClient with OcpiConfiguration and needed endpoints. Then you can perform the requests like that:
use Chargemap\OCPI\Versions\V2_1_1\Client\Locations\GetListing\GetLocationsListingRequest; use Chargemap\OCPI\Versions\V2_1_1\Common\Models\Location; use Chargemap\OCPI\Common\Client\OcpiClient; use Chargemap\OCPI\Common\Client\OcpiConfiguration; use Chargemap\OCPI\Common\Client\OcpiEndpoint; use Chargemap\OCPI\Common\Client\OcpiVersion; use Chargemap\OCPI\Common\Models\BaseModuleId; $ocpiClient = new OcpiClient( (new OcpiConfiguration($supervisorAuthString)) ->withEndpoint(new OcpiEndpoint( OcpiVersion::V2_1_1(), BaseModuleId::LOCATIONS(), new Uri('ocpi/cpo2.0/locations')) ) ); $getLocationListingRequest = (new GetLocationsListingRequest()) ->withOffset(0) ->withLimit(100) ->withDateFrom($dateFrom) ->withDateTo($dateTo); $locationResponse = $ocpiClient->V2_1_1()->locations()->getListing($getLocationListingRequest); /** @var Location[] $locations */ $locations = $locationResponse->getLocations(); //Some code...
Listing request/response
use Chargemap\OCPI\Versions\V2_1_1\Client\Locations\GetListing\GetLocationsListingRequest; use Chargemap\OCPI\Common\Client\OcpiClient; use Chargemap\OCPI\Common\Client\OcpiConfiguration; use Chargemap\OCPI\Common\Client\OcpiEndpoint; use Chargemap\OCPI\Common\Client\OcpiVersion; use Chargemap\OCPI\Common\Models\BaseModuleId; $ocpiClient = new OcpiClient( (new OcpiConfiguration($supervisorAuth)) ->withEndpoint(new OcpiEndpoint( OcpiVersion::V2_1_1(), BaseModuleId::LOCATIONS(), new Uri('ocpi/cpo2.0/locations')) ) ); $getLocationListingRequest = (new GetLocationsListingRequest()) ->withOffset(0) ->withLimit(100) ->withDateFrom($dateFrom) ->withDateTo($dateTo); do { $locationResponse = $this->ocpiClient->V2_1_1()->locations()->getListing($getLocationListingRequest); //Some code... //Next request will update its limit and offset values $getLocationListingRequest = $locationResponse->getNextRequest(); } while ($getLocationListingRequest !== null);
Common
Errors
Each error class corresponds to an OCPI error code. It can be converted to the PSR-7 response instance just like any response class. It ensures correct HTTP error code as well as OCPI status code.
use Chargemap\OCPI\Common\Server\Errors\OcpiGenericClientError; use Psr\Http\Message\ResponseInterface; $error = new OcpiGenericClientError('Client error'); //Correct payload and HTTP error code is already set /** @var ResponseInterface $response */ $response = $error->getResponseInterface();
Errors are supposed to be thrown and caught by a middleware/listener and then transformed to the response.
Models
Models fetched from request's/response's json body correspond to the OCPI objects. The exception to the rule is Partial[Class] classes, that are used in PATCH routes. They have the same but nullable properties as their corresponding [Class].
mrbig/ocpi-protocol 适用场景与选型建议
mrbig/ocpi-protocol 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 09 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mrbig/ocpi-protocol 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mrbig/ocpi-protocol 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-07