phpgt/fetch 问题修复 & 功能扩展

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

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

phpgt/fetch

最新稳定版本:v1.2.2

Composer 安装命令:

composer require phpgt/fetch

包简介

Asynchronous HTTP client with promises.

README 文档

README

Asynchronous HTTP client, implementation of the Fetch Standard which defines requests, responses, and the process that binds them: fetching.

See also, the JavaScript implementation that ships as standard in all modern browsers.

Build status Code quality Code coverage Current version PHP.Gt/Fetch documentation

Example usage: compute multiple HTTP requests in parallel, using fetch

<?php
$http = new Gt\Fetch\Http();

// Rather than creating the request now, `fetch` returns a Promise, 
// for later resolution with the BodyResponse.
$http->fetch("http://example.com/api/something.json")
->then(function(BodyResponse $response) {
// The first Promise resolves as soon as a response is received, even before
// the body's content has completed downloading.
	if(!$response->ok) {
		echo "Looks like there was a problem. Status code: "
			. $response->getStatusCode() . PHP_EOL;
		return null;
	}

// Within this Promise callback, you have access to the body stream, but
// to access the contents of the whole body, return a new Promise here:
    return $response->json();
})
->then(function(Json $json) {
// The second Promise resolves once the whole body has completed downloading.
    echo "Got JSON result length "
    	. count($json->results)
    	. PHP_EOL;

// Notice that type-safe getters are available on all Json objects:
    echo "Name of first result: "
    	. $json->results[0]->getString("name")
    	. PHP_EOL;
});

// A third request is made here to show a different type of body response:
$http->fetch("http://example.com/something.jpg")
->then(function(BodyResponse $response) {
    return $response->blob();
})
->then(function($blob) {
    echo "Got JPG blob. Saving file." . PHP_EOL;
    file_put_contents("/tmp/something.jpg", $blob);
});

// Once all Promises are registered, all HTTP requests can be initiated in
// parallel, with the callback function triggered when they are all complete. 
$http->all()->then(function() {
    echo "All HTTP calls have completed!" . PHP_EOL;
});

For more extensive examples, check out the code in the example directory.

Proudly sponsored by

JetBrains Open Source sponsorship program

JetBrains logo.

统计信息

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

GitHub 信息

  • Stars: 36
  • Watchers: 2
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-09-28