ddrv/http-client

无cURL依赖的简单HTTP客户端

v2.0.3 2020-12-14 12:47 UTC

This package is auto-updated.

Last update: 2024-09-14 20:45:53 UTC


README

Latest Stable Version Total Downloads License PHP

ddrv/http-client

无cURL依赖的简单HTTP客户端。需要在php.ini文件中启用allow_url_fopen选项。

; php.ini
allow_url_fopen = On

安装

安装此包,你喜欢的psr-7实现和你喜欢的psr-17实现

composer require ddrv/http-client:^2.0

使用

<?php

use Ddrv\Http\Client\Client;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseFactoryInterface;

/** 
 * @var ResponseFactoryInterface $responseFactory
 * @var int $timeout = 60
 * @var int $redirects = 0
 */
$http = new Client($responseFactory, $timeout, $redirects);

/** @var RequestInterface $request */
$response = $http->sendRequest($request);

$code = $response->getStatusCode();
$phrase = $response->getReasonPhrase();
$headers = $response->getHeaders();
$someHeader = $response->getHeader('Content-Type');

$body = $response->getBody()->__toString();

配置

<?php

use Ddrv\Http\Client\Client;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\UriInterface;

/**
 * @var ResponseFactoryInterface $responseFactory
 * @var UriInterface $proxy
 */

$http = new Client($responseFactory);

$http->setFollowRedirects(0); // Set 0 follow redirects (disable). 
$http->setTimeOut(10); // Set connection timeout 10 seconds

$http->setProxy($proxy); // Set proxy
$http->setProxy(); // Unset proxy

SSL授权

<?php

use Ddrv\Http\Client\Client;
use Psr\Http\Message\ResponseFactoryInterface;

/**
 * @var ResponseFactoryInterface $responseFactory
 */

$http = new Client($responseFactory);

$http->setSslAuth('host', 'ssl.crt', 'ssl.key'); // without password
$http->setSslAuth('host', 'ssl.crt', 'ssl.key', 'p@s$w0rd'); // with password
$http->unsetSslAuth('host'); // disable