anvilm/php.transport 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

anvilm/php.transport

最新稳定版本:v3.0.0

Composer 安装命令:

composer require anvilm/php.transport

包简介

README 文档

README

This package is a client for PHP sockets and allows establishing TCP and UDP connections.

Supported protocols:

  • TCP
  • UDP
  • TLS

Installation

You can install this package using Composer:

composer require anvilm/php.transport

Basic Usage

Create a client

TCP Client

Create a TCP client:

use AnvilM\Transport\Client

$client = Client::tcp('example.com:80');

UDP Client

Create a UDP client:

use AnvilM\Transport\Client

$client = Client::upd('example.com:80');

TLS Client

Create a TLS (SSL) client:

use AnvilM\Transport\Client

$client = Client::tls('example.com:80');

Open connection

To open a connection, use the open method:

use AnvilM\Transport\Client

$client = Client::tcp('example.com:80');

$timeout = 3; // Timeout in seconds

$client->open($timeout);

Send packets

To send packets, you can use the write method:

use AnvilM\Transport\Client

$client = Client::tcp('example.com:80');

$data = 'hello'; // Data to send
$timeout = 30; // Write timeout
$length = strlen($data); // Length of data to send

// Open connection and send packet
$client->open()->write(pack('V', $data), $length);

Read packets

To read received data, use the read method:

use AnvilM\Transport\Client

$client = Client::tcp('example.com:80');

$length = 1024; // Length of data for read
$timeout = 30; // Timeout to wait for data from server

// Open connection and read data
$data = $client->open()->read($length, $timeout);

Close connection

After closing the connection, you can immediately open a similar one, as a new socket is created upon closing.

use AnvilM\Transport\Client

$client = Client::tcp('example.com:80')->open()
    ->close()
    ->open();

Context

ВYou can also use a context if necessary. This code creates and sets parameters for stream_context_create. Currently, all context parameters are available for TCP, UDP, and TLS (SSL) connections.

You can also modify this context, as the context resource will be changed by reference.

use AnvilM\Transport\Client
use AnvilM\Transport\Connection\Context\TLSContext;

$context = (new TLSContext())->timeout(5.5)
    ->verifyPeer(true)
    ->verifyPeerName(true);
        
$client = Client::tls('example.com:443', $context);

$client->open(); // Open with timeout 5.5

$context->timeout(10); // Set new timeout

$data = $client->read(); // Read data with new timeout

Socket

You can get the socket object if you need manual connection management:

use AnvilM\Transport\Client
use AnvilM\Transport\Connection\Socket\Socket;

$client = Client::tcp('example.com:443');

$socket = $client->getSocket();

$socket->open()->enableCrypto();

统计信息

  • 总下载量: 15
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-08