spindle / httpclient

ext-curl的简单封装,支持多请求

1.0.1 2014-09-15 13:56 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:13:52 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()的封装

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

__clone()

Spindle\HttpClient\Request是可克隆的。克隆后,所有选项都会被复制。

$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

响应的封装。

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_*的封装。可以实现并行请求。

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";
}

许可证

spindle/httpclient的版权已放弃。使用时没有限制,也不需要联系作者或版权声明。可以复制代码片段来使用。

许可证原文

CC0-1.0 (无保留权利)