library co-httpclient
httpclient by coroutines
hirak/co-httpclient
httpclient by coroutines
- Thursday, September 18, 2014
- by hirak
- Repository
- 6 Watchers
- 16 Stars
- 1 Installations
- PHP
- 0 Dependents
- 0 Suggesters
- 3 Forks
- 0 Open issues
- 1 Versions
- 0 % Grown
hiraku/co-httpclient
yield(generator)でcurl_multiをうまいこと書けるようにした関数のサンプル実装。
まだ結構バグバグなので、本番利用は危険です。, (*1)
install
requirement:, (*2)
$ composer require hirak/co-httpclient
使い方
<?php
require 'vendor/autoload.php';
use Spindle\HttpClient;
/**
* JSONを返すWebAPIをGETメソッドで叩く関数。
*
* - 待ちが発生するところにyieldキーワードを埋め込む。
* - returnの代わりにyieldキーワードを使う。
* - 例外は通常通り使用可能。
*/
function getWebapiAsync($url) {
$req = new HttpClient\Request($url);
/** @type HttpClient\Response */
$res = (yield $req);
if (($status = $res->getStatusCode()) >= 400) {
throw new \RuntimeException($url, $status);
}
yield json_decode($res->getBody());
}
//試しにpackagist.orgのjsonを取ってきてパースしてみる
// json_decodeなどはなるべくWebAPIの待ち時間中に処理されます。
list($jpmirror, $origin) = co(
getWebapiAsync('http://composer-proxy.jp/proxy/packagist/packages.json'),
getWebapiAsync('https://packagist.org/packages.json')
);
var_dump($jpmirror, $origin);
license
CC0-1.0 (Public Domain), (*3)
dev-master
9999999-dev
httpclient by coroutines
Sources
Download
CC0-1.0
The Requires
The Development Requires
by
Hiraku NAKANO