Services
FEAST simplifies working with HTTP requests and APIs. It includes a Request class for both Curl or file_get_contents based requests.
In addition, you may add your own Request class by implementing \Feast\Interfaces\HttpRequestInterface
The chosen class is specified in your configuration file as service.class
and is used in all Service classes.
Sample Service class request usage
The HttpRequestInterface
The HttpRequestInterface contains methods for working with web requests. This class has a fluent interface on the builder methods.
The Builder Methods
-
get
- This method initializes a GET request and takes a URL as an argument. -
post
- This method initializes a POST request and takes a URL as an argument. -
put
- This method initializes a PUT request and takes a URL as an argument. -
patch
- This method initializes a PATH request and takes a URL as an argument. -
delete
- This method initializes a DELETE request and takes a URL as an argument. -
postJson
- This method initializes a POST request, sets the content type toapplication/json
and takes a URL as an argument. -
putJson
- This method initializes a PUT request, sets the content type toapplication/json
and takes a URL as an argument. -
patchJson
- This method initializes a PATCH request, sets the content type toapplication/json
and takes a URL as an argument. -
addCookie
- This method takes a name and value of a cookie to be used on the request. -
addArgument
- This method takes a name, value and a boolean flag of whether the argument should be treated as an array. -
addArguments
- This method takes a pre-built array of arguments to be used on the request. Only allows simple key => value mappings. -
clearArguments
- Clears all arguments. -
setReferer
- Set the referer [sic] for the request. -
setUserAgent
- Set the user agent for the request. -
authenticate
- This method takes a username and password to be used to authenticate the request. -
addHeader
- This method takes a header name and value to be sent on the request. -
makeRequest
- This method executes the request.
Request Metadata Methods
-
getReferer
- Get the referer [sic] for the request. -
getCookies
- Get the cookies for the request as an array. -
getContentType
- Get the content type of the request. -
getHeaders
- Get all the headers as an array. -
getResponseCode
- Get the HTTP response code for the last request.
Response Methods
-
getResponse
- Gets the\Feast\Response
object associated with the request. -
getResponseAsString
- Gets the response as a string -
getResponseAsXml
- Gets the response as a SimpleXMLElement. -
getResponseAsJson
- Get the response as a stdClass from a JSON string. -
getResponseCode
- Get the HTTP response code for the response.
Sample Service class request usage
$this->httpRequest->postJson(self::URL . '/subscribers');
$this->httpRequest->addArguments($data);
$this->httpRequest->authenticate($this->apiKey, '');
$this->httpRequest->makeRequest();
$response = $this-httpRequest->getResponseAsJson();
Powered by FEAST Framework
See this project at https://github.com/feastframework/documentation