机场锋 / request-url
RequestUrl 是一个 PHP HTTP 客户端库
v1.5
2023-08-09 10:43 UTC
Requires
- php: >=5.3
- ext-curl: *
README
RequestUrl 是一个 PHP HTTP 客户端库。
RequestUrl 允许您发送 GET、POST、PUT、DELETE HTTP 请求。您可以启用 Cookie、启用 gzip、设置用户代理、设置引用者、设置超时、设置连接超时和设置代理。您可以使用简单的数组添加头信息、表单数据以及参数,并以相同的方式访问响应数据。
RequestUrl 使用 cURL,但它将所有繁琐的操作抽象化,提供了一个简单的 API。
安装
composer require jichangfeng/request-url
用法
// Complex request $result = RequestUrl::getInstance() ->enableCookie('test.tmp') ->enableGzip(true) ->setUserAgent('Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36') ->setReferer('https://example.com') ->setTimeout(10) ->setConnectTimeout(5) ->setHeaders(array('Accept' => 'application/json')) ->setParams(array('name' => 'admin', 'password' => '123456')) ->setProxy('127.0.0.1', 1080) ->post('https://www.example.com/user/login'); // Simple request $result = RequestUrl::getInstance()->get('https://www.example.com'); $result = RequestUrl::getInstance()->setParams(array('key' => 'value'))->post('https://www.example.com'); // application/x-www-data-urlencoded // application/x-www-form-urlencoded is the default form submission format. // When using cURL to send a request, if the Content-Type is not set, this format will be used by default. $result = RequestUrl::getInstance() ->setParams(array('key' => 'value')) ->post('https://www.example.com'); // But if this header is set manually, cURL will send the request body as a plain text string instead of form-encoded. // At this time, you must use the http_build_query() function to manually build form data. $result = RequestUrl::getInstance() ->setHeaders(array('Content-Type' => 'application/x-www-data-urlencoded')) ->setParams(http_build_query(array('key' => 'value'))) ->post('https://www.example.com'); // multipart/form-data $result = RequestUrl::getInstance() ->setHeaders(array('Content-Type' => 'multipart/form-data')) ->setParams(array('avatar' => new CURLFile('/tmp/avatar.png'), 'nickname' => 'coco')) ->post('https://www.example.com'); // application/json $result = RequestUrl::getInstance() ->setHeaders(array('Content-Type' => 'application/json')) ->setParams(json_encode(array('key' => 'value'))) ->post('https://www.example.com'); var_dump($result['body']); // string '[...]'... (length=1270) var_export($result['header']); /* 'HTTP/2 200 cache-control: max-age=604800 content-type: text/html; charset=UTF-8 date: Tue, 13 Aug 2019 10:26:21 GMT etag: "1541025663+ident" expires: Tue, 20 Aug 2019 10:26:21 GMT last-modified: Fri, 09 Aug 2013 23:54:35 GMT server: ECS (dcb/7F38) vary: Accept-Encoding x-cache: HIT content-length: 1270 ' */ $headers = RequestUrl::getInstance()->parseHeader($result['header']); var_export($headers); /* array ( 'cache-control' => 'max-age=604800', 'content-type' => 'text/html; charset=UTF-8', 'date' => 'Tue, 13 Aug 2019 10:26:21 GMT', 'etag' => '"1541025663+ident"', 'expires' => 'Tue, 20 Aug 2019 10:26:21 GMT', 'last-modified' => 'Fri, 09 Aug 2013 23:54:35 GMT', 'server' => 'ECS (dcb/7F38)', 'vary' => 'Accept-Encoding', 'x-cache' => 'HIT', 'content-length' => '1270', ) */ var_export($result['info']); /* array ( 'url' => 'https://www.example.com', 'httpCode' => 200, 'contentType' => 'text/html; charset=UTF-8', 'contentTypeDownload' => 1270.0, 'sizeDownload' => 1270.0, 'elapsedTime' => 1.7826, 'errno' => 0, 'error' => '', 'proxy' => false, ) */