定制 michaelhall/http-client 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

michaelhall/http-client

最新稳定版本:v2.1.0

Composer 安装命令:

composer require michaelhall/http-client

包简介

A simple HTTP client

关键字:

README 文档

README

Tests StyleCI License Latest Stable Version Total Downloads

A simple HTTP client.

Requirements

  • PHP >= 8.0

Install with Composer

$ composer require michaelhall/http-client

Basic usage

Get a web page

<?php

require_once __DIR__ . '/vendor/autoload.php';

use DataTypes\Net\Url;
use MichaelHall\HttpClient\HttpClient;
use MichaelHall\HttpClient\HttpClientRequest;

$url = Url::parse('https://example.com/');
$client = new HttpClient();
$request = new HttpClientRequest($url);
$response = $client->send($request);

// Prints "success" if request was successful, "fail" otherwise.
echo $response->isSuccessful() ? 'success' : 'fail';

// Prints the response content.
echo $response->getContent();

// Prints the http code, e.g. 200.
echo $response->getHttpCode();

// Prints the headers.
foreach ($response->getHeaders() as $header) {
    echo $header;
}

Customize the request

<?php

require_once __DIR__ . '/vendor/autoload.php';

use DataTypes\Net\Url;
use DataTypes\System\FilePath;
use MichaelHall\HttpClient\HttpClientRequest;

$url = Url::parse('https://example.com/');

// Set the method.
$request = new HttpClientRequest($url, 'POST');

// Set a POST field.
$request->setPostField('Foo', 'Bar');

// Set a file.
$request->setFile('Baz', FilePath::parse('/path/to/file'));

// Add a header.
$request->addHeader('Content-type: application/json');

// Set raw content.
$request->setRawContent('{"Foo": "Bar"}');

// Client certificates.
$request->setCACertificate(FilePath::parse('/path/to/ca-certificate.pem'));
$request->setClientCertificate(FilePath::parse('/path/to/client-certificate.pem'));
$request->setClientKey(FilePath::parse('/path/to/client-key.pem'));

Create a custom request handler

A custom/fake request handler may be used and injected in the HttpClient constructor. To do this, the request handler must implement RequestHandlerInterface and the handleRequest method.

<?php

use DataTypes\Net\Url;
use MichaelHall\HttpClient\HttpClient;
use MichaelHall\HttpClient\HttpClientRequest;
use MichaelHall\HttpClient\HttpClientRequestInterface;
use MichaelHall\HttpClient\HttpClientResponse;
use MichaelHall\HttpClient\HttpClientResponseInterface;
use MichaelHall\HttpClient\RequestHandlers\RequestHandlerInterface;

require_once __DIR__ . '/vendor/autoload.php';

class FakeRequestHandler implements RequestHandlerInterface
{
    public function handleRequest(HttpClientRequestInterface $request): HttpClientResponseInterface
    {
        if ($request->getUrl()->getPath()->__toString() === '/foo') {
            return new HttpClientResponse(200, 'Hello World');
        }

        return new HttpClientResponse(404);
    }
}

// Inject the custom request handler in constructor.
$client = new HttpClient(new FakeRequestHandler());

$request = new HttpClientRequest(Url::parse('https://example.com/foo'));
$response = $client->send($request);

// Prints "Hello World".
echo $response->getContent();

$request = new HttpClientRequest(Url::parse('https://example.com/'));
$response = $client->send($request);

// Prints "404".
echo $response->getHttpCode();

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-01-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固