graftype/speedtest-php

Speedtest.net for PHP | 库和命令行界面

1.0.12 2024-04-25 12:34 UTC

This package is auto-updated.

Last update: 2024-09-25 13:28:27 UTC


README

库和命令行界面,用于通过 Speedtest.net 从命令行或网页界面运行服务器端速度测试。

demo

此库是 aln-1/speedtest-php 的更新版本,从 Python 版本 sivel/speedtest-cli 转换而来。

Latest Version Total Downloads License PHP Version

导航

安装

使用 Composer 将库 安装到现有项目 的方法

由于我们的存储库是公开包,我们已将此存储库列在 Packagist 上,这意味着我们不需要指导 Composer 查找哪个 GitHub 存储库以查找包。

作为项目依赖项

  • 使用以下命令安装库
cd /path-to-app/
composer require graftype/speedtest-php

通过更新项目的 composer.json 文件

  • 在项目文件夹 /path-to-app/ 中打开名为 composer.json 的文件

  • 将以下内容添加到该文件中

{
    "require": {
        // ---
        // Other packages
        // ---
        "graftype/speedtest-php": "^1.0"
    }
}
  • 之后,使用以下命令安装库
cd /path-to-app/
composer update
  • 带有现有 composer.json 文件的 /path-to-app/ 项目文件夹

如何使用 Composer 将库 安装到新项目

当您需要使用 Composer 将库 安装到新项目 时,请使用以下命令

  • 如果您的服务器/本地机器上未安装 Composer,请按照以下 安装指南 进行操作

  • 在项目文件夹中创建一个空文件,命名为 composer.json

  • 将以下内容添加到该文件中

{
    "require": {
        "graftype/speedtest-php": "^1.0"
    }
}
  • 之后,使用以下命令安装库
cd /path-to-app/
composer update
  • 带有现有 composer.json 文件的 /path-to-app/ 项目文件夹

更新

当您需要将库更新到最新版本时,请使用以下命令

cd /path-to-app/
composer update
  • 带有现有 composer.json 文件的 /path-to-app/ 项目文件夹

示例

常规用法

最小化代码

代码

require 'vendor/autoload.php';

$speedtest = new Graftype\Speedtest\Speedtest();
$speedtest->getServers();
$speedtest->getBestServer();
$speedtest->download();
$speedtest->upload();

$results = $speedtest->results();

print_r($results);

输出

Graftype\Speedtest\Result Object
(
    [latency:protected] => 4.57
    [download:protected] => 47888585.578516
    [upload:protected] => 64841042.860629
    [bytesReceived:protected] => 59881235
    [bytesSent:protected] => 82579808
)

单位

  • latency = ms
  • download / upload = bits/s
  • bytesReceived / bytesSent = bytes

带有进度回调

代码

require 'vendor/autoload.php';

$config = new Graftype\Speedtest\Config();
$config->setCallback(function ($results) { print_r($results); });

$speedtest = new Graftype\Speedtest\Speedtest($config);
$speedtest->getServers();
$speedtest->getBestServer();
$speedtest->download();

输出

Graftype\Speedtest\Result Object
(
    [latency:protected] => 4.40
    [download:protected] => 0
    [upload:protected] =>
    [bytesReceived:protected] => 0
    [bytesSent:protected] => 0
)

// More dump was here ...

```console
Graftype\Speedtest\Result Object
(
    [latency:protected] => 4.40
    [download:protected] => 27519752.835724
    [upload:protected] =>
    [bytesReceived:protected] => 37153149
    [bytesSent:protected] => 0
)

单位

  • latency = ms
  • download / upload = bits/s
  • bytesReceived / bytesSent = bytes

我们在此仅显示下载方法的第一和最后一个回调结果。

网络演示

一页演示可在 resources/demo.php 中找到

speedtest-php demo 2

命令行界面用法

控制台

/path-to-app/vendor/bin/speedtest -h

输出

usage: speedtest [-h] [--no-download] [--no-upload] [--single] [--bytes]
                 [--share] [--simple] [--json] [--list] [--server=SERVER]
                 [--exclude=EXCLUDE] [--source=SOURCE] [--timeout=TIMEOUT]
                 [--proxy=PROXY] [--version]

Command line interface to run server-side speedtests via Speedtest.net from cli or web interface.
--------------------------------------------------------------------------
https://github.com/graftype/speedtest-php

optional arguments:
  -h, --help            Show this help message and exit
  --no-download         Do not perform download test
  --no-upload           Do not perform upload test
  --single              Only use a single connection instead of multiple. This
                        simulates a typical file transfer
  --bytes               Display values in bytes instead of bits. Does not
                        affect the image generated by --share, nor output from
                        --json
  --share               Generate and provide a URL to the speedtest.net share
                        results image
  --simple              Suppress verbose output and progress, only shows results
  --json                Output in JSON format. Speeds listed in bits and not
                        affected by --bytes. Can be combined with --simple
                        to supress progress
  --list                Display a list of speedtest.net servers sorted by
                        distance
  --server=SERVER       Specify a server ID to test against. Can be comma
                        separated values
  --exclude=EXCLUDE     Exclude a server from selection. Can be comma
                        separated values
  --source=SOURCE       Source IP address to bind to or interface name
  --timeout=TIMEOUT     HTTP timeout in seconds, default 10
  --proxy=PROXY         Use a proxied connection for test
  --version             Show the version number
  • /path-to-app/ 项目文件夹

支持

这是 Speedtest.net for PHP 的开发者门户,不应用于支持。如果您需要提交支持请求,请通过网站聊天 联系我们

付费咨询和个人帮助

如果您需要帮助您的项目,请通过网站聊天 联系我们,我们将尽力为您找到最佳解决方案。