1 Unstar Star 0 Fork 3

tongjiekeji / LeafPHPMIT

forked from ShirDon-廖显东 / Leaf 
Create your Gitee Account
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

Leaf 文档

Leaf是一个简单而轻量级的HTTP客户端,易于使用。此页是文档的索引。请使用下面的目录开始阅读。

浏览器对象:Browser

浏览器是发送HTTP请求的高级对象。主要关注的是简单性。

在构造“Browser”时,必须选择要使用的[客户端](/doc/Client.md)。这个

默认情况下使用FileGetContentsclient。参见如下:

use Leaf\Browser;
use Leaf\Client\FileGetContents;
use Nyholm\Psr7\Factory\Psr17Factory;

$client = new FileGetContents(new Psr17Factory());
$browser = new Browser($client, new Psr17Factory());

$response = $browser->get('https://example.com');
$response = $browser->get('https://example.com', ['User-Agent'=>'Leaf']);
$response = $browser->post('https://example.com', ['User-Agent'=>'Leaf'], 'http-post-body');

$response = $browser->head('https://example.com');
$response = $browser->patch('https://example.com');
$response = $browser->put('https://example.com');
$response = $browser->delete('https://example.com');


$response = $browser->request('GET', 'https://example.com');

You do also have a function to send PSR-7 requests.

use Nyholm\Psr7\Request;

$request = new Request('GET', 'https://google.com/foo');
$response = $browser->sendRequest($request)

提交表单

有了Leaf,你已经建立了对请求表单的支持。你当然可以创建自己的PSR-7请求并发布,但使用Browser::submit()`函数或'FormRequestBuilder'可能更容易。

下面是如何使用“Browser::submit()”上载文件的示例。

$browser->submitForm('https://example.com/foo', [
    'user' => 'Kris Wallsmith',
    'image' => [
        'path'=>'/path/to/image.jpg'
      ],
]);
$browser->submitForm('https://example.com/foo', [
    'user[name]' => 'Kris Wallsmith',
    'user[image]' => [
        'path'=>'/path/to/image.jpg',
        'filename' => 'my-image.jpg',
        'contentType' => 'image/jpg',
      ],
]);

使用 FormRequestBuilder

如果您有一个大的from,或者您希望以结构化的方式构建您的请求,那么您可以使用“FormRequestBuilder”。

use Leaf\Message\FormRequestBuilder;

$builder = new FormRequestBuilder();
$builder->addField('user[name]', 'Kris Wallsmith');
$builder->addFile('user[image]', '/path/to/image.jpg', 'image/jpg', 'my-image.jpg');
$builder->addFile('cover-image', '/path/to/cover.jpg');

$browser->submitForm('https://example.com/foo', $builder->build());

Continue reading about Clients.

PHP
1
https://gitee.com/tongjiekeji/Leaf.git
git@gitee.com:tongjiekeji/Leaf.git
tongjiekeji
Leaf
Leaf
master

Search

132457 8cb2edc1 1899542 131848 70c8d3a4 1899542