gyron/net2web

Net2Web 客户端库

v1.0.2 2018-01-05 13:50 UTC

This package is not auto-updated.

Last update: 2024-09-22 08:27:36 UTC


README

PHP 客户端库用于 Net2Web

关于

Net2,由 Paxton 开发,是门禁控制系统开发商和提供商。他们的软件在 Windows 上运行,并且有一个 SDK(在 .NET 中),但是没有提供任何远程可访问的 API。Net2Web 与 Net2 一起运行,并将 SDK 的约 90% 功能作为 XML 可访问 API 暴露。

此库旨在简化与 Net2Web 暴露的 API 的集成。

安装说明

Composer

通过命令行简单地执行

composer require gyron/net2web

或者,您可以通过在您的 composer.json 中的 "require": { 部分添加一行来更新

"gyron/net2web" : "v1.0.2"

使用方法

快速入门

只需复制并粘贴以下代码,并调整值

$oNet2Encryption = new \Gyron\Net2Web\Encryption( 'ENCRYPTION KEY', \Gyron\Net2Web\Encryption::OpenSSL );
$oNet2Session = new \Gyron\Net2Web\Session( 'USERID', 'PASSWORD', 'NET2 SERVER IP', '7070', $oNet2Encryption );
$oNet2Client = new \Gyron\Net2Web\Client( $oNet2Session );

这将立即建立连接,但是会为每个请求创建一个新的 Net2Web 会话。
建议您使用 $oNet2Session->getSessionId(),并将其缓存(最多 2 到 4 小时)。然后可以通过将 Session ID 传递回 Session 实例来重用它
new Session( ..., ..., ..., ..., $sessionId );

高级实现

以下是库的示例实现,形式为服务工厂,它利用会话 ID 缓存到本地存储。

<?php declare( strict_types=1 );

namespace Gyron\Sample;

use Gyron\Net2Web\Client;
use Gyron\Net2Web\Encryption;
use Gyron\Net2Web\Session;

/**
 * Class AccessApiFactory
 * @package Gyron\Sample
 */
class AccessApiFactory {

  /**
   * @var string
   */
  private $sCachePath;

  /**
   * @param string $sCachePath
   */
  public function __construct( string $sCachePath ) {
    $this->sCachePath = $sCachePath;
  }

  /**
   * @param array $aConfig requires user_id, password, ip and port
   * @return Client
   * @throws \Exception
   */
  public function forConfig( array $aConfig ) {
    $sCacheFile = sprintf( '%s/net2web_session.sid', rtrim( $this->sCachePath, '/' ) );

    $sSessionId = null;
    if ( is_file( $sCacheFile ) ) {
      $sSessionId = trim( file_get_contents( $sCacheFile ) );
    }
        
    $oNet2Encryption = new Encryption( $aConfig['enckey']', Encryption::OpenSSL );
    $oNet2Session = new Session( $aConfig['user'], $aConfig['password'], $aConfig['host'], (string)$aConfig['port'], $oNet2Encryption, $sSessionId );
    if ( $sSessionId != $oNet2Session->getSessionId() ) {
      file_put_contents( $sCacheFile, trim( $oNet2Session->getSessionId() ) );
    }
    return ( new Client( $oNet2Session ) );
  }
}