klebervmv / easycurl
EasyCurl 是一个简化 Curl 请求使用的库
1.0.12
2023-12-19 16:12 UTC
Requires
- php: >=7.2
- ext-curl: *
- ext-json: *
README
安装
上传者可通过 Composer 获取
"klebervmv/easycurl": "1.0.*"
或者运行
composer require klebervmv/easycurl
文档
easyCurl 是为了简化简单请求的 curl 通信开发而成的,支持 GET、POST、PUT 和 DELEATE 等方法。同时允许通过 CURL 发送图片
构造详情
构造函数有 3 个参数,其中只有一个是必填的
第一个参数是基础 URL;
第二个是 ssl 验证 - 默认为 true;
第三个是 json 或 xml 的 post 类型 - 默认为 json。
从 1.0.10 版本开始,实现了发送请求而不等待它们返回的功能。为此,在 send 方法中插入 false 选项:send(false)。
<?php use klebervmv\EasyCurl; $easyCurl = new EasyCurl("route url", true, "json");
简单请求示例
<?php use klebervmv\EasyCurl; $easyCurl = new EasyCurl("route url"); $easyCurl->render("GET", "/ednpoint")->send(); //If there is an error in the communication, it will be returned in the getError() method; if($easyCurl->getError()){ var_dump($easyCurl->getError()); return; } //Through the getHttpCode() method you can validate the return http code if($easyCurl->getHttpCode() !== 200){ var_dump($easyCurl->getResult()); return; } //the result will be returned in the getResult() method in the array format var_dump($easyCurl->getResult());
通过传递参数的简单 POST 请求示例
参数可以作为数组或 stdClass 传递,因为它们将被转换为 Json
<?php use klebervmv\EasyCurl; $easyCurl = new EasyCurl("route url"); $param = new stdClass(); $param->firstName = "Kleberton"; $param->lastName = "Vilela"; $param->email = "exemple@exemple.com"; $easyCurl->render("POST", "/ednpoint", $param)->send(); //If there is an error in the communication, it will be returned in the getError() method; if($easyCurl->getError()){ var_dump($easyCurl->getError()); return; } //Through the getHttpCode() method you can validate the return http code if($easyCurl->getHttpCode() !== 200){ var_dump($easyCurl->getResult()); return; } //the result will be returned in the getResult() method in the array format var_dump($easyCurl->getResult());
插入头部
<?php use klebervmv\EasyCurl; $easyCurl = new EasyCurl("route url"); $easyCurl->render("GET", "/ednpoint") ->setHeader("Authorization:Bearer TOKEN") ->send();
重置并插入新的头部
您可以清除整个头部并插入新的头部参数
<?php use klebervmv\EasyCurl; $easyCurl = new EasyCurl("route url"); $easyCurl->render("GET", "/ednpoint") ->resetHeader() ->setHeader("lang:pt-BR") ->setHeader("Authorization:Bearer TOKEN") ->send();