dbeurive/http
Composer 安装命令:
composer require dbeurive/http
包简介
This package implements a simple REST requester
README 文档
README
This package implements a simple wrapper around the Curl library that makes the development of REST clients easier.
Installation
From the command line:
composer require dbeurive/http
Or, from within the file composer.json:
"require": {
"dbeurive/http": "*"
}
Synopsis
Basic usage
require_once '/path/to/autoload.php';
use dbeurive\Http\Requester;
use dbeurive\Http\ExceptionParameter;
use dbeurive\Http\ExceptionRuntime;
use dbeurive\Http\Code;
// ---------------------------------------------
// GET / DELETE
// ---------------------------------------------
// Create a requester.
// Through the constructor's second parameter, you can provide a setting that will be applied for all requests.
// Here, we set the required header for a basic authentication.
$general_curl_options = array(CURLOPT_USERPWD => 'login:password');
$requester = new Requester($general_curl_options);
$report = null;
try {
// Send a GET request.
// Through the method's second parameter, you can provide a setting that will be applied for this request only.
// The method returns an "execution report". This report contains:
// - the request that has been sent (the header and the content).
// - the response that has been received (the header and the body).
// - information returned by the Curl library (such as the request duration).
$specific_curl_options = array(CURLOPT_HTTPHEADER => array(
'header1: a',
'header2: b',
));
$report = $requester->doGet('http://www.google.com/search?q=php',
$specific_curl_options); // This parameter is optional.
} catch (ExceptionRuntime $e) {
printf("FATAL ERROR: %s\n", $e->getMessage());
exit(1);
} catch (ExceptionParameter $e) {
printf("FATAL ERROR: %s\n", $e->getMessage());
exit(1);
} catch (\Exception $e) {
printf("FATAL ERROR: %s\n", $e->getMessage());
exit(1);
}
$http_code = $report->getResponse()->getCode();
printf("Last HTTP response code: %d\n", $http_code);
printf("Content length: %d\n", $report->getResponse()->getContentLength());
printf("Content type: %s\n\n", is_null($report->getResponse()->getContentType()) ? 'null' : $report->getResponse()->getContentType());
if (Code::HTTP_OK != $http_code) {
printf("HTTP code: %d (%s)\n\n",
$http_code,
Code::getDescription($http_code)
);
}
// Get the request header. Please note that the request content will be null (as it is for any GET request).
printf("Request header:\n\n%s\n\n",
$report->getRequest()->getHeader()->getAsString()
);
// Get the response header.
printf("Response header:\n\n%s\n\n",
$report->getResponse()->getHeader()->getAsString()
);
// Get the response body.
printf("Request body:\n\n%s\n\n",
$report->getResponse()->getBody()
);
// Get data about the execution.
printf("Request body:\n\n%s\n\n",
$report->getInformation()->getAsString(true)
);
// ---------------------------------------------
// POST / PUT
// ---------------------------------------------
// Create a requester.
// Note: a requester is not bound to a URL or a method. We could have reused the one you've previously created (for the
// GET example).
$general_curl_options = array(); // No setting is provided for all requests.
$requester = new Requester($general_curl_options); // Note: the parameter is optional.
define('HOST', 'localhost');
define('PORT', 8000);
$query_string = '/handle_post.php';
$url = sprintf('http://%s:%d%s', HOST, PORT, $query_string);
$content = 'var=value';
$specific_curl_options = array(CURLOPT_HTTPHEADER => array(
'header1: a',
'header2: b',
));
$report = null;
try {
$report = $requester->doPost($url,
$content,
'text/html',
$specific_curl_options); // This parameter is optional.
} catch (ExceptionRuntime $e) {
printf("FATAL ERROR: %s\n", $e->getMessage());
exit(1);
} catch (ExceptionParameter $e) {
printf("FATAL ERROR: %s\n", $e->getMessage());
exit(1);
} catch (\Exception $e) {
printf("FATAL ERROR: %s\n", $e->getMessage());
exit(1);
}
printf("Last HTTP response code: %d\n", $report->getResponse()->getCode());
printf("Content length: %d\n", $report->getResponse()->getContentLength());
printf("Content type: %s\n\n", is_null($report->getResponse()->getContentType()) ? 'null' : $report->getResponse()->getContentType());
// Get the request header. Please note that the request content will be null (as it is for any GET request).
printf("Request header:\n\n%s\n\n",
$report->getRequest()->getHeader()->getAsString()
);
// Get the request content.
printf("Request content:\n\n%s\n\n",
$report->getRequest()->getContent()
);
// Get the response header.
printf("Response header:\n\n%s\n\n",
$report->getResponse()->getHeader()->getAsString()
);
// Get the response body.
printf("Request body:\n\n%s\n\n",
$report->getResponse()->getBody()
);
// Get data about the execution.
printf("Request body:\n\n%s\n\n",
$report->getInformation()->getAsString(true)
);
Advanced usage
You can create your own customised requesters.
class MyRequester extends AbstractRequester {
// ...
/**
* Perform a GET request.
* @param string $in_url The URL.
* @param array $in_opt_additional_curl_options Additional CURL options.
* @return Report The execution report.
* @throws ExceptionParameter
* @throws ExceptionRuntime
*/
public function doGet($in_url, array $in_opt_additional_curl_options=array()) {
// ...
return parent::_doGet($in_url, $in_opt_additional_curl_options);
}
/**
* Perform a POST request.
* @param string $in_url The URL.
* @param string $in_content The request content.
* @param string $in_content_type The content type.
* @param array $in_opt_additional_curl_options $in_additional_curl_options Additional CURL options.
* @return Report The execution report.
* @throws ExceptionParameter
* @throws ExceptionRuntime
*/
public function doPost($in_url, $in_content, $in_content_type, array $in_opt_additional_curl_options=array()) {
// ...
return parent::_doPost($in_url, $in_content, $in_content_type, $in_opt_additional_curl_options);
}
/**
* Perform a PUT request.
* @param string $in_url The URL.
* @param string $in_content The request content.
* @param string $in_content_type The content type.
* @param array $in_opt_additional_curl_options $in_additional_curl_options Additional CURL options.
* @return Report The execution report.
* @throws ExceptionParameter
* @throws ExceptionRuntime
*/
public function doPut($in_url, $in_content, $in_content_type, array $in_opt_additional_curl_options=array()) {
// ...
return parent::_doPut($in_url, $in_content, $in_content_type, $in_opt_additional_curl_options);
}
/**
* Perform a DELETE request.
* @param string $in_url The URL.
* @param array $in_opt_additional_curl_options Additional CURL options.
* @return Report The execution report.
* @throws ExceptionParameter
* @throws ExceptionRuntime
*/
public function doDelete($in_url, array $in_opt_additional_curl_options=array()) {
// ...
return parent::_doDelete($in_url, $in_opt_additional_curl_options);
}
// ...
}
Examples
The directory "examples" contains several examples:
Please note that in order to run these examples, you need to start PHP as a WEB server, as described below:
cd tests
php -S localhost:8000 -t public
dbeurive/http 适用场景与选型建议
dbeurive/http 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 11 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「request」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dbeurive/http 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dbeurive/http 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dbeurive/http 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
HTTP request logger middleware for Laravel
Adds request-parameter validation to the SLIM 3.x PHP framework
http客户端
Easily add Excepct-CT header to your project
A simple HTTP application builder using PSR-15 HTTP Server Request Handler and Middleware.
统计信息
- 总下载量: 6
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-11-01