asika / http
Requires
- php: >=5.3.10
- psr/http-message: 1.*
- windwalker/uri: ~2.0
Requires (Dev)
- windwalker/environment: ~2.0
- windwalker/test: ~2.0
Provides
This package is auto-updated.
Last update: 2019-02-21 09:17:25 UTC
README
此包提供了 PSR7 标准的 Http 消息对象、Uri 对象、Stream 对象和客户端请求对象。(兼容 PHP 5.3)
本包的部分代码基于 phly/http 和 joomla/http
项目已弃用,请参阅 windwalker/http
通过 Composer 安装
将以下内容添加到您的 composer.json
文件的 require 块中。
{ "require": { "asika/http": "~1.0" } }
发起请求
HttpClient 是一个用于发起 restful 请求的简单类。
use Asika\Http\HttpClient; $http = new HttpClient; $response = $http->get('http://example.com/?foo=bar'); // This is PSR7 ResponseInterface (string) $response->getBody();
其他方法
$http = new HttpClient; // The post data can be query string or array $response = $http->post('http://example.com/?foo=bar', array('post_data' => 'data')); $response = $http->put('http://example.com/?foo=bar', array('post_data' => 'data')); $response = $http->patch('http://example.com/?foo=bar', array('post_data' => 'data')); $response = $http->delete('http://example.com/?foo=bar', array('post_data' => 'data')); $response = $http->head('http://example.com/?foo=bar'); $response = $http->trace('http://example.com/?foo=bar'); $response = $http->options('http://example.com/?foo=bar'); // With headers $response = $http->get('http://example.com/', null, array('X-Foo' => 'Bar')); // Use request() $response = $http->request('POST', 'http://example.com/?foo=bar', 'this=is&post=data');
使用 Psr RequestInterface 发起请求
Psr7 Request 是一个不可变对象,每次操作都必须获取返回的对象。
use Asika\Http\Request; $request = new Request; // Note: You have to get the return value. // Every change will return new object. $request = $request->withRequestTarget('http://example.com/flower/sakura') ->withMethod('POST') ->withAddedHeader('Authorization', 'Bearer ' . $token) ->withAddedHeader('Content-Type', 'application/text'); // OR $request = new Request( 'http://example.com/flower/sakura', 'POST', 'php://memory', array( 'Authorization' => 'Bearer ' . $token, 'Content-Type' => 'application/json', ) ); // This is a POST request so we write post data to body $request->getBody()->write('this=is&post=data'); $http = new HttpClient; // Send request $response = $http->send($request);
使用 Uri 和 Json 输出。
use Asika\Http\Request; use Asika\Http\Uri\PsrUri; $request = (new Request) ->withUri(new PsrUri('http://example.com')) ->withMethod('POST') ->withAddedHeader('Authorization', 'Bearer ' . $token) ->withAddedHeader('Content-Type', 'application/json') // Use JSON // Note: Request will ignore path and query in Uri // So we have to set RequestTarget here ->withRequestTarget('/path/of/uri?flower=sakura'); // If you want to set a non-origin-form request target, set the // request-target explicitly: $request = $request->withRequestTarget((string) $uri); // absolute-form $request = $request->withRequestTarget($uri->getAuthority(); // authority-form $request = $request->withRequestTarget('*'); // asterisk-form // This is JSON request so we encode data here $request->getBody()->write(json_encode($data)); $response = $http->send($request); $response->getStatusCode(); // 200 is OK
自定义传输和选项
现在支持 Curl 和 Steam 2 传输。
use Asika\Http\Transport\CurlTransport; $options = array( 'certpath' => '/custom/cert.pem' ); $transport = new CurlTransport($options); // Set transport when client new $http = new HttpClient(array(), $transport);
设置自定义 CURL 选项
$options = array( 'options' => array( CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => true ) ); $httpOptions = array( 'headers' => array( 'X-Foo' => 'Bar' ) ); $http = new HttpClient($httpOptions, new CurlTransport($options));
下载远程文件
$http = new HttpClient; $dest = '/path/to/local/file.zip'; $response = $http->download('http://example.com/file.zip', $dest); if ($response->getStatusCode() != 200) { // Error }
响应接口
响应对象包含一个 Stream 对象来存储返回的字符串。
// The return value is: 'FOO BAR' $body = $response->getBody(); // Simply to string (string) $body; // FOO BAR $body->seek(2); $body->getContents(); // O BAR $body->rewind(); $body->read(5); // FOO B $body->getSize(); // 7
Uri
Uri
是一个简单的 Uri 对象,用于修改 URL,但不是 Psr UriInterface。
Uri
类提供的方法允许您操纵 uri 的所有方面。例如,如果您想设置一个新的 uri,添加端口号,然后发送用户名和密码来验证 .htaccess 安全文件,可以使用以下语法
// new uri object $uri = new Asika\Http\Uri\Uri; $uri->setHost('http://localhost'); $uri->setPort('8888'); $uri->setUser('myUser'); $uri->setPass('myPass'); echo $uri->__toString();
这将输出
myUser:myPass@http://localhost:8888
如果您想在主机之后添加特定的文件路径,可以使用 setPath()
方法
// set path $uri->setPath('path/to/file.php');
这将输出
myUser:myPass@http://localhost:8888path/to/file.php
添加 URL 查询
// url query $uri->setQuery('foo=bar');
输出
myUser:myPass@http://localhost:8888path/to/file.php?foo=bar
PsrUri
PsrUri
是实现了 Psr UriInterface 的 Uri 对象。
此对象也是不可变的,因此每次更改都必须获取新的返回值。
$uri = (new PsrUri('http://example.com')) ->withScheme('https') ->withUserInfo('user', 'pass') ->withPath('/path/to/target') ->withQuery('flower=sakura') ->withFragment('#hash'); (string) $uri; // https://user:[email protected]/path/to/target?flower=sakura#fragment
Stream
Stream 是一个强大的流封装器。
写入数据到内存
$stream = new Stream('php://memory', 'wb+'); $stream->write('Foo Bar'); $stream->rewind(); // Back to begin // Now we take something we wrote into memory $stream->__toString(); // get: Foo Bar // OR $stream->rewind(); $stream->getContents(); // get: Foo Bar
从 php://input
读取数据
$stream = new PhpInputSteam; $data = $stream->__toString(); // foo=bar $query = \Asika\Http\Uri\UriHelper::parseQuery($data); // array('foo' => 'bar')
读取文件
$stream = new Stream('/path/to/file.txt', 'r+'); $stream->__toString(); // Read $steam->seek($stream->getSize()); $steam->write('new string'); // Write
快速复制流。
// Remote source $src = new Stream('http://example/test.txt'); // Local store $dest = new Stream('/path/to/local/test.txt'); // Do copy \Asika\Http\Helper\StreamHelper::copy($src, $dest); // Get Data $dest->rewind(); $data = $dest->getContents();
参见: Psr7 StreamInterface / API
其他 Http 消息对象
ServerRequest
用于存储服务器数据的请求对象,例如:$_SERVER
、$_COOKIE
、$_REQUEST
等。
UploadedFile
用于存储上传文件的对象,详见:上传文件接口
$files = array(); foreach ($_FILE as $name => $file) { $files[$name] = new UploadedFile($file['tmp_name'], $file['size'], $file['error'], $file['name'], $file['type']); } $request = new ServerRequest( $_SERVER, $_GET, $_POST, $_COOKIE, $files );