spindle/httpclient

simple wrapper for ext-curl, multi-request enabled

1.0.1 2014-09-15 13:56 UTC

This package is not auto-updated.

Last update: 2024-03-12 00:32:48 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version Total Downloads Latest Unstable Version License

curl_*関数をモダンなPHPらしく書けるようにした薄いラッパークラスです。 curl_multi_*に対応しており、並列リクエストが可能です。

$request = new Spindle\HttpClient\Request('http://example.com/api', array(
  'post' => true,
  'postFields' => http_build_query(array(
    'param' => 'value',
  )),
));

$response = $request->send();

$statusCode = $response->getStatusCode();
$header     = $response->getHeaderString();
$body       = $response->getBody();
$body       = (string)$response;
<?php
//libcurl original
$ch = curl_init('http://example.com/api');
curl_setopt_array($ch, array(
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HEADER => true,
  CURLOPT_POST => true,
  CURLOPT_POSTFIELDS => http_build_query(array(
    'param' => 'value',
  )),
));

$response_fulltext = curl_exec($ch);
curl_close($ch);

Spindle\HttpClient\Request

curl_init()のWrapperです。

__construct([ $url, [ array $options ] ])

__clone()

Spindle\HttpClient\Requestはclone可能です。cloneした場合、オプションなどがすべてコピーされます。

$req1 = new Spindle\HttpClient\Request('http://example.com/');
$req2 = clone $req1;

void setOption($label, $value)

curl_setopt()のラッパーです。 デフォルトでCURLOPT_RETURNTFANSFERCURLOPT_HEADERはtrueに設定されているため、改めてセットする必要はありません。 CURLOPT_定数は、文字列でも書くことができます。

$req = new Spindle\HttpClient\Request;

//equals
$req->setOption(CURLOPT_POST, true);
$req->setOption('post', true);

//equals
$req->setOption(CURLOPT_POSTFIELDS, 'a=b');
$req->setOption('postFields', 'a=b');

文字列がラベルに指定された場合、全て大文字にして、CURLOPT_をくっつけてから該当する定数を探します。

void setOptions(array $options)

curl_setopt_array()のラッパーです。setOption()と同じく、文字列ラベルが使えます。

$req = new Spindle\HttpClient\Request();
$req->setOptions(array(
  'post' => true,
  'postFields' => 'a=b',
));

Spindle\HttpClient\Response send()

リクエストを送信し、レスポンスが返るまで待ちます。

Spindle\HttpClient\Response getResponse()

最後に取得したレスポンスを返します。

Spindle\HttpClient\Response

レスポンスのWrapperです。

int getStatusCode()

HTTPのステータスコードを返します。

string getUrl()

リクエストに使われたURLを返します。

string getContentType()

レスポンスのContent-Typeを返します。

string getContentLength()

レスポンスのContent-Lengthを返します。

mixed getInfo(string $label)

curl_getinfo()のラッパーです。

string getHeaderString()

レスポンスヘッダーの文字列を返します。

mixed getHeader(string $headerName = null)

$headerNameに対応するレスポンスヘッダーの中身を返します。 $headerNameを省略すると、レスポンスヘッダーを連想配列形式で返します。

string getBody()

レスポンスボディの文字列を返します。

Spindle\HttpClient\Multi

curl_multi_*のWrapperです。並列リクエストを行うことができます。

use Spindle\HttpClient;

$pool = new HttpClient\Multi(
    new HttpClient\Request('http://example.com/api'),
    new HttpClient\Request('http://example.com/api2')
);
$pool->setTimeout(10);

$pool->send(); //wait for all response

foreach ($pool as $url => $req) {
    $res = $req->getResponse();
    echo $url, PHP_EOL;
    echo $res->getStatusCode(), PHP_EOL
    echo $res->getBody(), PHP_EOL;
}
use Spindle\HttpClient;

$pool = new HttpClient\Multi;
$req1 = new HttpClient\Request('http://example.com/api1');
$req2 = new HttpClient\Request('http://example.com/api2');

$pool->attach($req1);
$pool->attach($req2);

$pool->detach($req1);

$pool->send();

send()は全てのリクエストを送り、全てのレスポンスが戻ってくるのを待ちますが、これをstart()waitResponse()の二つに分けて書くと、待っている間に他のコードを実行できます。

なお、start()は失敗することがあり、-1を返します。その場合は何度か実行してみてください。(複数回実行に副作用はありません)

use Spindle\HttpClient;

$pool = new HttpClient\Multi(
    new HttpClient\Request('http://example.com/api'),
    new HttpClient\Request('http://example.com/api2')
);

$pool->start();

for ($i=0; $i<10000; $i++) {
    very_very_heavy_function();
    $pool->start();
}

$pool->waitResponse();

foreach ($pool as $req) {
    $res = $req->getResponse();
    echo "{$res->getStatusCode()}\t{$res->getUrl()}\t{$res->getBody()}\n";
}

License

spindle/httpclientの著作権は放棄するものとします。 利用に際して制限はありませんし、作者への連絡や著作権表示なども必要ありません。 スニペット的にコードをコピーして使っても問題ありません。

ライセンスの原文

CC0-1.0 (No Rights Reserved)