composer 安装:composer require ethansmart/httpbuilder
github 地址:https://github.com/roancsu/httpbuilder
在PHP 框架中可以使用 guzzlehttp 来构建HTTP Request 服务,但是guzzle 太重了,用起来比较繁琐,所以我用curl封装了PHP HTTP Request Client,支持 GET,POST,PUT,DELETE 方法,并且对文件上传有安全检测功能等等,使用也非常简单,效率高。
Usage:
构建 HttpClient
protected $client ;
function __construct()
{
$this->client = HttpClientBuilder::create()->build();
}
GET Request
$data = [
'uri'=>'https://www.baidu.com'
];
return $this->client
->setHeaders('Content-Type:application/json')
->setHeaders('X-HTTP-Method-Override:GET')
->setHeaders('Request_id: Ethan')
->setTimeout(10)
->Get($data);
POST Request
$data = [
'uri'=>'https://www.baidu.com',
'params'=> [
'user'=>ethan
]
];
return $this->client
->setHeaders('Content-Type:application/json')
->Post($data);
PUT 、DELETE Request
$data = [
'uri'=>'https://www.baidu.com',
'params'=> [
'user'=>ethan
]
];
return $this->client
->setHeaders('Content-Type:application/json')
->Put($data); // Delete($data)
扩展
文件上传
Q:
在使用 composer 安装过程中会出现 如下异常:
[InvalidArgumentException]
Could not find a version of package ethansmart/es-for-laravel matching your minimum-stability (stable). Require it with an explicit version constraint allowing its desired stability.
解决方法:
在项目composer.json文件中加入:
"repositories": [
{
"packagist.org": false
},
{
"type": "composer",
"url": "https://packagist.org"
}
],
将国内的composer镜像换成 packagist.org 就可以了。