spindle\httpclient
, (*1)
curl_*関数をモダンなPHPらしく書けるようにした薄いラッパークラスです。
curl_multi_*に対応しており、並列リクエストが可能です。, (*2)
$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です。, (*3)
__construct([ $url, [ array $options ] ])
__clone()
Spindle\HttpClient\Requestはclone可能です。cloneした場合、オプションなどがすべてコピーされます。, (*4)
$req1 = new Spindle\HttpClient\Request('http://example.com/');
$req2 = clone $req1;
void setOption($label, $value)
curl_setopt()
のラッパーです。
デフォルトでCURLOPT_RETURNTFANSFER
とCURLOPT_HEADER
はtrueに設定されているため、改めてセットする必要はありません。
CURLOPT_
定数は、文字列でも書くことができます。, (*5)
$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_
をくっつけてから該当する定数を探します。, (*6)
void setOptions(array $options)
curl_setopt_array()
のラッパーです。setOption()と同じく、文字列ラベルが使えます。, (*7)
$req = new Spindle\HttpClient\Request();
$req->setOptions(array(
'post' => true,
'postFields' => 'a=b',
));
Spindle\HttpClient\Response send()
リクエストを送信し、レスポンスが返るまで待ちます。, (*8)
Spindle\HttpClient\Response getResponse()
最後に取得したレスポンスを返します。, (*9)
Spindle\HttpClient\Response
レスポンスのWrapperです。, (*10)
int getStatusCode()
HTTPのステータスコードを返します。, (*11)
string getUrl()
リクエストに使われたURLを返します。, (*12)
string getContentType()
レスポンスのContent-Typeを返します。, (*13)
string getContentLength()
レスポンスのContent-Lengthを返します。, (*14)
mixed getInfo(string $label)
curl_getinfo()
のラッパーです。, (*15)
レスポンスヘッダーの文字列を返します。, (*16)
$headerNameに対応するレスポンスヘッダーの中身を返します。
$headerNameを省略すると、レスポンスヘッダーを連想配列形式で返します。, (*17)
string getBody()
レスポンスボディの文字列を返します。, (*18)
Spindle\HttpClient\Multi
curl_multi_*
のWrapperです。並列リクエストを行うことができます。, (*19)
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()
の二つに分けて書くと、待っている間に他のコードを実行できます。, (*20)
なお、start()
は失敗することがあり、-1を返します。その場合は何度か実行してみてください。(複数回実行に副作用はありません), (*21)
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の著作権は放棄するものとします。
利用に際して制限はありませんし、作者への連絡や著作権表示なども必要ありません。
スニペット的にコードをコピーして使っても問題ありません。, (*22)
ライセンスの原文, (*23)
CC0-1.0 (No Rights Reserved)
- https://creativecommons.org/publicdomain/zero/1.0/
- http://sciencecommons.jp/cc0/about (Japanese), (*24)