ptcong / php-http-class
此包已被弃用且不再维护。未建议替代包。
一个简单而强大的Http客户端库,用于发送请求并获取类似浏览器的响应。
3.0.10
2016-04-07 04:12 UTC
Requires
- php: >=5.0
README
此包已过时,请使用 EasyRequest 代替。它更新且更好。
简单的PHP Http客户端
一个简单的Http库,受PSR-7启发,并使用curl/socket发送请求。
要求
PHP需要至少是PHP 5.0版本,且启用了socket或curl。
安装
要安装此库,请安装composer并执行以下命令
composer require "ptcong/php-http-class": "^3.0"
composer update
如果你的主机正在运行PHP 5.2,你应该参考此库: https://bitbucket.org/xrstf/composer-php52
用法
首先,你需要包含composer自动加载器。
require dirname(__FILE__).'/vendor/autoload.php';
- 创建客户端
- 选项和辅助方法
- 一些简单选项(超时、协议版本、用户代理等...)
- 带有头信息的发送
- 带有cookie的发送
- 带有查询字符串的发送
- 带有表单参数的发送
- 带有多部分数据的发送
- 上传文件
- 发送原始数据
- 发送JSON数据
- 使用HTTP/Sock代理
- 使用基本认证
- 发送和获取响应
创建客户端
$client = EasyRequest::create('GET', 'http://google.com'); // or $client = EasyRequest::create('http://google.com', 'GET'); $client = EasyRequest::create('http://google.com'); // default is GET
使用默认选项创建客户端
$method = 'POST'; // may GET/POST/PUT or any HTTP method $target = 'http://domain.com'; $request = EasyRequest::create($method, $target, array( 'handler' => null, // null|string - "socket" or "curl". null to use default. 'method' => 'GET', // string 'url' => null, // string 'nobody' => false, // boolean 'follow_redirects' => 0, // integer|true - True to follows all of redirections. 'protocol_version' => '1.1', // string 'timeout' => 10, // integer Timeout in seconds 'user_agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:38.0) Gecko/20100101 Firefox/38.0', 'auth' => null, // null|string An Auth Basic "user:password" 'proxy' => null, // null|string A proxy with format "ip:port" 'proxy_userpwd' => null, // null|string User password with format "user:password" 'proxy_type' => 'http', // string Must be one of "http" or "sock5" 'headers' => array(), // array 'cookies' => array(), // array 'json' => false, // false|string String json 'body' => '', // string|resource 'query' => array(), // array 'form_params' => array(), // array 'multipart' => array(), // array ))->send(); var_dump($request->getResponse()); // null if have errors occured while sending.
选项和辅助方法
此库提供两种发送请求的处理程序:Socket和Curl。默认情况下,库将尝试检测您的PHP设置和您设置的请求选项以提供处理程序。但是,如果您想使用Socket或Curl,您可以通过handler
选项指定。Socket是PHP内置的,因此您可以使用此库发送请求而不需要curl扩展。
$client = EasyRequest::create('POST', 'http://domain.com', array( 'handler' => 'socket' // or 'curl' ));
创建和发送请求的快捷方法
您可以使用所有HTTP方法作为快捷方式
$client = EasyRequest::post('http://domain.com', $options); $client = EasyRequest::get('http://domain.com', $options); $client = EasyRequest::put('http://domain.com', $options); $client = EasyRequest::delete('http://domain.com', $options); ...
一些简单选项
$client ->withTimeout(10) // timeout of sending request ->withNobody(true) // specify that you only want to get headers ->withProtocolVersion('1.1') // HTTP protocol version ->withFollowRedirects(true) // true or an integer ->withUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:38.0) Gecko/20100101 Firefox/38.0');
带有头信息
$client = EasyRequest::create('POST', 'http://domain.com', array( 'headers' => array( 'Referer' => 'http://google.com', 'Header1' => 'value', 'Header2' => array( 'value2', 'value3' ) ) )) // to append value2 to Header1 ->withHeader('Header1', 'value2') // to replace value3 to Header1 ->withHeader('Header1', 'value3', flase) // to append value4, value5 to Header1 ->withHeader('Header1', array('value4', 'value5')) // to append value 7 to Header2, value6 to Header1 ->withHeader(array( 'Header1' => 'value6', 'Header2' => 'value7' ), null, true) // to remove a header by case-insensitive ->withoutHeader('header1');
带有cookie
使用辅助方法设置动态值。
$client = EasyRequest::create('POST', 'http://domain.com', array( 'cookies' => array( array( 'Name' => 'cookie0', 'Value' => 'value0', 'Path' => '/', 'Max-Age' => 300, 'Expires' => time() + 86400, 'Secure' => false, 'Discard' => false, 'HttpOnly' => false ) ) )) // with default args ->withCookie('cookie1', 'value1', $path = '/', $secure = false, $httpOnly = false) ->withCookie('cookie2', 'value2') ->withCookie('cookie6=value6; expires=Fri, 26-Jun-2015 03:24:07 GMT') // Sets multiple cookies by string ->withStringCookies('cookie3=value3; cookie4=value4;cookie5=value5') // to remove a cookie by name ->withoutCookie('cookie1');
带有查询字符串
查询选项类似于表单参数选项。
$client = EasyRequest::create('POST', 'http://domain.com', array( 'query' => array( 'query1' => 'value1', 'query2' => 'value2', 'query3' => 'value3', ) )) ->withQuery('query1', 'value2') ->withQuery('query2', 'value3', false) // to override query2 ->withQuery('query3=value3&query4=value4') ->withQuery(array( 'query5' => 'value5', 'query6' => 'value6' )) // to remove a query by name ->withoutQuery('query1');
带有表单参数
$client = EasyRequest::create('POST', 'http://domain.com', array( 'form_params' => array( 'field1' => 'value1', 'field2' => array('value2', 'value3') 'field3' => array( 'nested1' => 1, 'nested2' => 2 ), 'field4' => array(1, 2) ) )) ->withFormParam('field1', 'value2') // field1 will be field1[]=value1&field1[]=value2 ->withFormParam('field1', 'value3', false) // field1 will be field1=value3 ->withFormParam('field5=value5&field6=value6&field7=value7') ->withFormParam(array( 'field8' => 'value8', 'field9' => 'value9' )); // to remove a form field by name ->withoutFormParam('field') // to see your data var_dump((string) $client->prepareRequest()->getBody());
带有多部分数据
多部分字段需要name
和contents
键。 filename
和headers
是可选的。
$client = EasyRequest::create('POST', 'http://domain.com', array( 'multipart' => array( array( 'name' => 'field1', 'contents' => 'value1' ), array( 'name' => 'field2', 'contents' => 'this is a text file', 'filename' => 'file.txt', 'headers' => array( 'Custom-Header' => 'abc' ) ), // may use to upload a file array( 'name' => 'field2', 'contents' => fopen('/path/to/file'), // optional keys 'filename' => 'file.jpg', 'headers' => array( 'Content-Type' => 'image/jpg' ) ) ) )) ->withMultipart('field2', 'value2') ->withMultipart('field3', 'value3', 'fieldname3') ->withMultipart('field4', 'value4', 'fieldname4', array('Custom-Header' => 'value')) ->withMultipart('file1', fopen('/path/to/file'), 'filename1') // to upload a file // to remove a part ->withoutMultipart('field2');
上传文件。
$client ->withFormFile('file1', '/path/to/file1', $optionalFileName = null, $optionalHeaders = array()) ->withFormFile('file2', '/path/to/file2'); // to remove this file $client->withoutMultipart('file1');
发送原始数据
$client->withBody('raw data');
发送JSON数据
用于轻松地将JSON编码数据作为请求的主体。如果消息上没有Content-Type头,将添加Content-Type: application/json
头。
$client->withJson(array(1,2,3)); // or $client->withJson(json_encode(array(1,2,3)));
带有HTTP/ Sock5代理
您可以使用HTTP或Sock代理。但Sock代理需要curl扩展。
$client = EasyRequest::create('POST', 'http://domain.com', array( 'proxy' => '192.168.1.105:8888', 'proxy_userpwd' => 'user:pass', 'proxy_type' => 'http' // "http" or "sock5" )) $client->withProxy('192.168.1.105:8888'); // proxy without user pass, default is HTTP proxy $client->withProxy('192.168.1.105:8888', 'user:pass'); // use HTTP proxy with user, pass $client->withProxy('192.168.1.105:8888', null, 'sock5'); // use sock5 proxy $client->withSock5Proxy('192.168.1.105:8888', 'user:pass'); // use sock5 proxy $client->withHttpProxy('192.168.1.105:8888', 'user:pass'); // use http proxy
带有基本认证
$client = EasyRequest::create('POST', 'http://domain.com', array( 'auth' => 'user:pass', )) $client->withAuth('user:pass');
发送请求并获取响应
$client->send(); var_dump($client->getResponse() !== null); boolean true
获取响应状态码
var_dump($client->getResponseStatus()); int 200
获取响应原因
var_dump($client->getResponseReason()); string 'OK' (length=2)
获取响应正文文本
var_dump($client->getResponseBody()); string 'Hello' (length=5) var_dump((string) $client); string 'Hello' (length=5)
将响应cookie作为字符串获取
var_dump($client->getResponseCookies()); string 'c1=v1; c2=v2;' (length=13)
将响应cookie作为数组获取
var_dump($client->getResponseArrayCookies()); array (size=2) 0 => array (size=9) 'Name' => string 'c1' (length=2) 'Value' => string 'v1' (length=2) 'Domain' => null 'Path' => string '/' (length=1) 'Max-Age' => null 'Expires' => string 'Sun, 28-Jun-2015 11:13:07 GMT' (length=29) 'Secure' => boolean false 'Discard' => boolean false 'HttpOnly' => boolean false 1 => array (size=9) 'Name' => string 'c2' (length=2) 'Value' => string 'v2' (length=2) 'Domain' => string 'abc.com' (length=7) 'Path' => string '/Path/' (length=6) 'Max-Age' => null 'Expires' => string 'Sun, 28-Jun-2015 11:13:07 GMT' (length=29) 'Secure' => boolean true 'Discard' => boolean false 'HttpOnly' => boolean true
通过指定名称获取响应头行
var_dump($client->getResponseHeader('set-cookie')); // Case-insensitive array (size=2) 0 => string 'c1=v1; expires=Sun, 28-Jun-2015 11:13:48 GMT' (length=44) 1 => string 'c2=v2; expires=Sun, 28-Jun-2015 11:13:48 GMT; path=/Path/; domain=abc.com; secure; httponly' (length=91)
将响应头行作为逗号分隔的字符串获取
var_dump($client->getResponseHeaderLine('server')); string 'Apache' (length=6)
将响应头作为行获取
var_dump($client->getResponseHeaders()); array (size=9) 0 => string 'Date: Sun, 28 Jun 2015 11:20:14 GMT' (length=35) 1 => string 'Server: Apache' (length=14) 2 => string 'X-Powered-By: PHP/5.2.17' (length=24) 3 => string 'Set-Cookie: c1=v1; expires=Sun, 28-Jun-2015 11:21:54 GMT' (length=56) 4 => string 'Set-Cookie: c2=v2; expires=Sun, 28-Jun-2015 11:21:54 GMT; path=/Path/; domain=abc.com; secure; httponly' (length=103) 5 => string 'Location: c.php' (length=15) 6 => string 'Content-Length: 0' (length=17) 7 => string 'Connection: close' (length=17) 8 => string 'Content-Type: text/html' (length=23)
获取跟随的重定向URL、计数、请求、cookie集合
var_dump($client->getRedirectedCount()); var_dump($client->getRedirectedUrls()); // all cookies, may has some different sites var_dump($client->getRedirectedCookies()); // get all request details var_dump($client->getRedirectedRequests());
获取调试信息
var_dump($client->getDebugInfo()); array (size=4) 'time_start' => float 1435488809.58 'time_process' => float 0.00152993202209 'handler' => string 'socket' (length=6) 'errors' => array (size=0) empty