YurunHttp v3.2.3 发布,支持 JSON 类型的 RequestBody

栏目: 软件资讯 · 发布时间: 6年前

内容简介:YurunHttp 是开源的 PHP HTTP 类库,支持链式操作,简单易用。 支持所有常见的 GET、POST、PUT、DELETE、UPDATE 等请求方式,支持上传下载、设置和读取 header、Cookie、请求参数、失败重试、限速、代理、证书等。...

YurunHttp 是开源的 PHP HTTP 类库,支持链式操作,简单易用。

支持所有常见的 GET、POST、PUT、DELETE、UPDATE 等请求方式,支持上传下载、设置和读取 header、Cookie、请求参数、失败重试、限速、代理、证书等。

3.0 版完美支持 Curl、Swoole 协程;3.2 版支持 Swoole WebSocket 客户端。

git 仓库中 test 目录里是示例代码!

更新日志(v3.2.3):

  • 支持 JSON 类型的 RequestBody
use Yurun\Util\HttpRequest;

$http = new HttpRequest;
$response = $http->post('http://www.xxx.com', [
    'id'    =>  1,
], 'json');
echo 'html:', PHP_EOL, $response->body();

Composer

本项目可以使用composer安装,遵循psr-4自动加载规则,在你的 composer.json 中加入下面的内容

{
    "require": {
        "yurunsoft/yurun-http": "~3.2"
    }
}

然后执行 composer update 安装。

之后你便可以使用 include "vendor/autoload.php"; 来自动加载类。(ps:不要忘了namespace)

用法

简单调用

<?php
use Yurun\Util\HttpRequest;

$http = new HttpRequest;
$response = $http->ua('YurunHttp')
                 ->get('http://www.baidu.com');

echo 'html:', PHP_EOL, $response->body();

PSR-7 请求构建

<?php
use Yurun\Util\YurunHttp\Http\Request;
use Yurun\Util\YurunHttp;

$url = 'http://www.baidu.com';

// 构造方法定义:__construct($uri = null, array $headers = [], $body = '', $method = RequestMethod::GET, $version = '1.1', array $server = [], array $cookies = [], array $files = [])
$request = new Request($url);

// 发送请求并获取结果
$response = YurunHttp::send($request);

var_dump($response);

Swoole 协程模式

<?php
use Yurun\Util\YurunHttp;
use Yurun\Util\HttpRequest;

// 设置默认请求处理器为 Swoole
YurunHttp::setDefaultHandler('Yurun\Util\YurunHttp\Handler\Swoole'); // php 5.4
// YurunHttp::setDefaultHandler(\Yurun\Util\YurunHttp\Handler\Swoole::class); // php 5.5+

// Swoole 处理器必须在协程中调用
go('test');

function test()
{
    $http = new HttpRequest;
    $response = $http->get('http://www.baidu.com');
    echo 'html:', PHP_EOL, $response->body();
}

WebSocket Client

YurunHttp::setDefaultHandler(\Yurun\Util\YurunHttp\Handler\Swoole::class);
go(function(){
    $url = 'ws://127.0.0.1:1234/';
    $http = new HttpRequest;
    $client = $http->websocket($url);
    if(!$client->isConnected())
    {
        throw new \RuntimeException('Connect failed');
    }
    $client->send('data');
    $recv = $client->recv();
    var_dump('recv:', $recv);
    $client->close();
});

具体详见examples目录中的示例代码

相关地址:


以上所述就是小编给大家介绍的《YurunHttp v3.2.3 发布,支持 JSON 类型的 RequestBody》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Discrete Mathematics and Its Applications

Discrete Mathematics and Its Applications

Kenneth H Rosen / McGraw-Hill Science/Engineering/Math / 2003-04-22 / USD 132.81

Discrete Mathematics and its Applications is a focused introduction to the primary themes in a discrete mathematics course, as introduced through extensive applications, expansive discussion, and deta......一起来看看 《Discrete Mathematics and Its Applications》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具