php-comp/http-client 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

php-comp/http-client

Composer 安装命令:

composer require php-comp/http-client

包简介

simple http client library of the php

README 文档

README

License Php Version Latest Stable Version GitHub tag (latest SemVer) Github Actions Status

An easy-to-use HTTP client library for PHP. Support CURL, file, fsockopen, stream drivers.

  • Simple and easy to use HTTP client
  • Support drivers: curl swoole fsockopen stream fopen
  • Support all HTTP method. eg: GET,POST,PATCH,PUT,HEAD,DELETE
  • Support setting proxy, customizing headers, auth, content-type etc.
  • Implement interface PSR 18

安装

composer require phppkg/http-client

使用

创建客户端实例

自动选择驱动类:

use PhpPkg\Http\Client\Client;

// use factory
$client = Client::factory([
    'driver' => 'curl', // stream, fsock, fopen, file, co, co2
    
    // ... 更多选项
    'baseUrl' =>  'http://my-site.com'
]);

直接使用指定的类:

$options = [
  'baseUrl' =>  'http://my-site.com'
  // ...
];
$client = CurlClient::create($options);
$client = FileClient::create($options);
$client = FSockClient::create($options);
$client = FOpenClient::create($options);
$client = CoClient::create($options);

基本使用

use PhpPkg\Http\Client\Client;

$client = Client::factory([]);

$client->get('/users/1');

$post = ['name' => 'john'];
$client->post('/users/1', $post);

// add ajax header
$client->byAjax()->post('/users/1', $post);

// add json content type
$client->json('/users/1', json_encode($post));
// or
$client->byJson()->post('/users/1', json_encode($post));

$statusCode = $client->getStatusCode();
$headers = $client->getResponseHeaders();

// get body data
$data = $client->getResponseBody();
$array = $client->getArrayData();

解析响应Body:

$data = $client->getDataObject();
$data->getInt('createTime', 0);

$user = new User();
$client->bindBodyTo($user);
vdump($user->name);

文件上传下载

  • public function upload(string $url, string $field, string $filePath, string $mimeType = '')
  • public function download(string $url, string $saveAs)
  • public function downloadImage(string $imgUrl, string $saveDir, string $rename = '')
$client = CurlClient::create([
  // ...
]);

$client->upload(...);

常用方法

  • getJsonArray/getArrayData(): array
  • getJsonObject(): stdClass
  • getDataObject(): DataObject
  • bindBodyTo(object $obj): void

LICENSE

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-11-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固