kite / 回弹
cURL 的简单对象包装器
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-22 03:34:31 UTC
README
cURL 的简单对象包装器
cURLBack 是一个简单的 cURL 包装器,允许存储过去的请求以供将来使用,而不需要重新请求资源。cURLBack 还提供了一个简单的面向对象的接口来创建和管理这些请求。
安装
您可以使用 Composer 安装 cURLBack。
{
"require": {
"kite/curlback": "1.*"
}
}
快速入门
使用 cURLBack 非常简单。以下是一个创建请求并打印响应的简单示例。
$myRequest = new Kite\CurlBack\Curl('http://www.google.com');
$myRequest->makeRequest();
print_r($myRequest->getResponse());
您可以通过逐个添加或通过数组轻松添加 GET 变量。
$myRequest = new Kite\CurlBack\Curl('http://www.google.com');
$myRequest->setGetValue("variable","value");
$myRequest->makeRequest();
print_r($myRequest->getResponse());
$myRequest = new Kite\CurlBack\Curl('http://www.google.com');
$getValues = array(
"variable" => "value",
"variable2" => "value,
);
$myRequest->setGetValue($getValues);
$myRequest->makeRequest();
print_r($myRequest->getResponse());
对于使用 setPostValue() 方法添加的 POST 变量也是如此。
您有多种方式可以与请求的响应信息进行交互。
returnResponse() 只提供请求的响应。returnRequestInfo() 提供响应信息以及响应头。returnHttpCode() 仅返回响应 HTTP 状态码
请求方法
与 cURL 类似,cURLBack 默认使用 GET 方法进行请求,但您可以使用 changeToPost()、changeToDelete()、changeToPut() 方法更改方法。还有一个 changeToCustom() 方法用于未记录的方法。
如果您发送 POST 变量,与 cURL 一样,cURLBack 将自动将其方法更改为 POST。
便捷方法
cURLBack 还具有 4 个便捷方法,允许您通过一次调用构建并发出请求。一个用于 GET、POST、PUT 和 DELETE。
它们看起来像以下这样
$curl = new Curl;
$curl->post('http://www.example.com', array(
'foo' => 'bar'
));
$curl->get('http://www.example.com', array(
'foo' => 'bar'
));
$curl->put('http://www.example.com', array(
'foo' => 'bar'
));
$curl->delete('http://www.example.com');
保存请求
cURLBack 内置了一个请求容器,可以存储您发出的每个请求的信息。
$myRequest = new Kite\CurlBack\Curl('http://www.google.com',true);
$myRequest->makeRequest();
print_r($myRequest->returnSavedRequests());
returnSavedRequests() 方法将返回一个对象,其中包含每个请求的所有信息以及完整的响应详细信息。您可以使用 returnRequestList() 和 returnRequestListWithTimes() 获取简化的列表,包括或不包括请求时间。
添加附加请求
要将附加请求添加到同一个 cURLBack 对象,只需添加新的请求地址,然后发出新的请求。
$myRequest = new Kite\CurlBack\Curl('http://www.google.com',true);
$myRequest->makeRequest();
$myRequest->setAddress('http://www.yahoo.com');
$myRequest->makeRequest();
一旦添加了新的请求地址,之前的请求将被移动到 pastResponses 属性中。要访问其信息,您需要调用它。
$myRequest = new Kite\CurlBack\Curl('http://www.google.com',true);
$myRequest->makeRequest();
$myRequest->setAddress('http://www.yahoo.com');
$myRequest->makeRequest();
$myRequest->pastResponses[0]['response'];
重放请求
您可以使用 replayRequest() 方法重放任何以前保存的请求。
$myRequest = new Kite\CurlBack\Curl('http://www.google.com',true);
$myRequest->makeRequest();
$myRequest->replayRequest(0);
清除请求历史记录
您可以使用 resetStoredResponses() 方法清除所有请求历史记录。
$myRequest = new Kite\CurlBack\Curl('http://www.google.com',true);
$myRequest->makeRequest();
$myRequest->resetStoredResponses();
头部 & 全局账户类型,用户
您还可以使用 setHeader() 方法设置 cURLBack 对象的头部。
$myRequest = new Kite\CurlBack\Curl('http://www.google.com',true);
$myRequest->setHeader("Content-type","application/json");
$myRequest->makeRequest();
默认情况下,cURLBack 设置全局接受类型头部为 'application/json',但您可以使用 setGlobalAccept() 方法更改它。
$myRequest = new Kite\CurlBack\Curl('http://www.google.com',true);
$myRequest->setGlobalAccept('application/html');
$myRequest->makeRequest();
此外,cURLBack 配置为存储和发送用于身份验证目的的用户头部属性。只需使用 setGlobalUser() 方法设置 $this->globalUser。
$myRequest = new Kite\CurlBack\Curl('http://www.google.com',true);
$myRequest->setGlobalUser('10ksd93023ksdfj0230kj23lk');
$myRequest->makeRequest();
批量请求
cURLBack 还支持使用主 cURL 对象的实例对请求进行批量处理。
$requests = array(
array(
"address"=>"http://www.kitewebconsulting.com",
"method"=>"GET",
)
);
$curl = new Curl("", true);
$batchRequest = new BatchHandler($requests, $curl);
$batchRequest->processRequests();
BatchHandler 具有扩展的接口,提供多种交互方式。以下是一个所有可用方法的列表。
__construct($requests, $curl) $requests 是一个包含请求数组的数组持有者。有关结构和要求的详细信息,请参见以下部分。
$curl 是用于发出请求和存储响应的 $curl 对象的实例。
processBatchObj($requests) 如果您想在 BatchHandler 对象实例化后添加请求,可以使用此方法传递请求对象。
addRequest($request, $position) 此方法允许您逐个添加请求。$request 对象是一个数组,其中包含一个请求数组,$position 是可选的,但可以用来覆盖现有请求。
clearRequests() 清除所有存储的请求。
processRequests($current) 这是调用请求的主要方法,一旦调用,将循环遍历请求对象,直到最后一个请求完成。
returnResponses() 为了避免必须调用您的 $curl 对象来获取所有响应,已设置一个快捷方式,以便您可以使用 $batchHandler 对象调用它们。
$requests 对象结构 下面是一个您可以选择的列表,以及哪些是必需的或可选的。
address (string - required)
method (string - required)
getValues (key value array - optional)
postValues (key value array - optional)
headers (key value array - optional)
accept (string - optional)
user (string - optional)
un (string - required if pw is set)
pw (string - required if un is set)
So a request with full options would look like this:
$requests = array(
array(
"address"=>"http://www.kitportal.com",
"method"=>"GET",
"getValues"=>array(
"key"=>"val"
)
"postValues"=>array(
"key"=>"val"
)
"headers"=>array(
"key"=>"val"
)
"accept"=>"application/json",
"user"=>"myUser",
"un"=>"username",
"pw"=>"password",
)
);
公共属性
$address = The active address to use when doing a request.
$getValues = An array holding all the get values of the active request.
$postValues = An array holding all the post values of the active request.
$method = The request method, POST, GET, PUT, DELTE or Custom
$storeRequests = Boolean for turning on or off request saving.
$pastResponses = Array holding all the past request and respones if storeRequests = true
$globalAccept = The header ACCEPT type to use on all requests.
$globalUser = The header USER to use on all requests if provided.
完整 API 列表
__construct($address, $storeRequests)
The construct can take an valid URL as the $address and a boolean as the
$storeRequests. Both are optional though.
setAddress($address)
This method sets the active request URL.
echoAddress()
This method simply prints the current active address.
setGetValue($name, $value)
The $name property can either be a string for the variable name or an array of
variables and values. $value is only used if $name is a variable string and holds
the value of that variable.
setPostValue($name, $value = '', $json = false)
The $name property can either be a string for the variable name or an array of
variables and values. $value is only used if $name is a variable string and holds
the value of that variable, unless you want to post a json object which case you
will set the object as the $value and set $json to true.
Example:
$jsonObj = '{"hi":"friend"}';
$request->setPostValue('', $jsonObj, true);
changeToPost()
Changes $method to POST
changeToPUT()
Changes $method to PUT
changeToDelete()
Changes $method to DELETE
changeToGet()
Changes $method to GET
customMethod($method)
The $method will change the request method to your custom method.
setHeader($name, $value)
The $name will be the name of the header while $value is the value of that header.
You can also just provide an array of headers in the name field to add bulk, just
leave the value field empty.
removeHeader($num)
This method will remove a header from the header list via the provided $num.
returnHeaderCount()
This method returns the total count of the provided headers.
setGlobalAccept($acceptType)
This method changes the header ACCEPT type value.
setGlobalUser($user)
This method sets the header USER property.
returnResponse()
This method will return the response from the last request.
returnHttpCode()
This method returns the HTTP code from the last request.
returnResponseInfo()
This method returns an array holding the response info and response headers.
Array (
"Response Info" : "info",
"Response Headers" : "info",
)
returnSavedRequests()
Returns an array of all the past responses.
setBasicAuth($un, $pw)
Sets basic authentication for the request with $un being the username and $pw being the password.
许可证
版权所有 (c) 2013, Kite, Inc 保留所有权利。
重新分发和使用,无论是源代码形式还是二进制形式,无论是修改还是未修改,只要满足以下条件:
- 源代码重新分发必须保留上述版权声明、本条件列表和以下免责声明。
- 二进制形式重新分发必须在使用于重新分发文档和/或其他材料中复制上述版权声明、本条件列表和以下免责声明。
- 未经事先书面许可,不得使用本软件的名称或其贡献者的名称来推广或认可由此软件派生出的产品。
本软件按“现状”提供,版权所有者和贡献者不提供任何明示或暗示的保证,包括但不限于适销性和针对特定目的的适用性保证。在任何情况下,版权所有者或贡献者不应对任何直接、间接、偶然、特殊、示范性或后果性损害(包括但不限于替代商品或服务的采购;使用、数据或利润的损失;或业务中断)承担责任,无论该损害是由于何种原因造成的,无论根据何种理论责任,即使在被告知可能发生此类损害的情况下。