hedii/http-punch

一个用于敲击(访问)HTTP端点的PHP库

2.0.1 2018-02-15 15:24 UTC

This package is auto-updated.

Last update: 2024-09-18 20:12:34 UTC


README

Build Status

http-punch

Http punch 是一个用于访问 HTTP URL 的 PHP 库。

它可以用来检查主机是否在线或离线,检查服务器响应时间,敲击定时任务 URL,敲击 Webhook URL 等...

目录

安装

通过 composer 安装

composer require hedii/http-punch

使用

实例化

创建一个 http punch 实例

<?php

// require composer autoloader
require '/path/to/vendor/autoload.php';

// instantiate
$puncher = new Hedii\HttpPunch\HttpPunch();

或者,你可以传递一个请求超时时间(默认30秒),以及一个连接超时时间(默认10秒)

// instantiate with request and connection timeout as parameters
$puncher = new Hedii\HttpPunch\HttpPunch(20, 5);

执行 HTTP 敲击

调用 punch(string $url, string $method = 'get', array $body = []): array 方法执行 HTTP 敲击。

// instantiate
$puncher = new Hedii\HttpPunch\HttpPunch();

// perform a get request
$result = $puncher->punch('http://example.com');

// perform a post request 
$result = $puncher->punch('http://example.com', 'post');

// perform a post request with a given body as an array
$result = $puncher->punch('http://example.com', 'post', ['foo' => 'bar']);

// set the outgoing ip address (it uses CURLOPT_INTERFACE behind the scenes) and perform a get request
$result = $puncher->setIp('192.160.0.101')->punch('http://example.com');

// set an array of request headers and perform a get request
$result = $puncher->setHeaders(['foo' => 'bar'])->punch('http://example.com');

此方法的结果是一个包含 HTTP 敲击报告信息的数组。成功 success 的值表示网站是否已成功响应请求

array(5) {
    'url' => "http://example.com"
    'success' => true
    'status_code' => 200
    'message' => "OK"
    'transfer_time' => 0.765217
}

注意,url 字段包含有效 URL(在重定向响应的情况下,这是最终 URL,状态码将为 200,消息为 "OK")。

结果数组

测试

composer test

许可证

hedii/http-punch 在 MIT 许可下发布。有关详细信息,请参阅附带LICENSE 文件。