Core API communication class
APCoreTx implements the core mechanism to send requests to the API and receive results. It takes care of setting the proper headers and sending parameters either as a http query (for GET and DELETE) or as url encoded data (for PUT and POST). Relies on the curl extension to make the request.
__construct(\APAPI\APAPI $pxAPIOb)
tx(string $pxURI, string $pxMethod, array $pxOptions = array()) : string
Send request to the API. Uses the credentials found in the API object and gets the result.
string
URI to request
string
HTTP verb to use (GET, DELETE, POST, PUT)
array
Data to transmit as url encoded values (only used with POST and PUT verbs)
string
call(string $httpMethod, string $url, array $uriComponents = array(), array $params = array(), mixed $file = false) : array
Send request to the API. Uses the credentials found in the API object and gets the result.
The final request URL is constructed from appending the url parameter to endPointBase, and adding any additional encoded URI componentes. For instance, to request:
loan/sections
The url parameter should contain the string 'loan/sections' and the call function takes care of the rest. Please note that the url parameter needs to be a valid url path, that is any of the following characters: A–Z, a–z, 0–9, -, ., _, ~, !, $, &, ', (, ), *, +, ,, ;, =, :, @, as well as % followed by two hexadecimal digits. Any other character that is part of the URI but NOT a query parameter must be percent encoded, as this is not handled automatically.
The additional uriComopnents parameter encodes a series of components of a URL, in the order they appear in the URL path. It may be used to specify the part of the final URL path that defines GET and DELETE parameters. In some occasions, these parameters contain invalid URL characters (such as dates in the format of MM/DD/YYYY the '/' character is invalid. Or when a parameter needs to be JSON encoded, the '{}' characters are invalid also) and they need to be encoded. For example, to get the revenue cash flow report you may use:
call("GET", "reports/forecast/revenuecashflow/%7B%7D");
Where '%7B%7D' is the percent encoded version of '{}' the empty JSON object. Or an easier way to call the same endpoint:
call("GET", "reports/forecast/revenuecashflow/", new array("{}");
In this case there's only one URI component that is encoded and appended tho the final URL. The main advantage of this method is that you don't need to do the encoding yourself. It's important to note that parameters are appended to the final URL in the order they were specified in the input array.
Most GET and DELETE endpoints expect their parameters to be part of the URL path. For instance:
loan/summary/1
Gets the summary for loan with the ID 1. These are required parameters, and shall be sent in the same url string. However there are other optional parameters that are sent as a query string. For example:
loan/search?expand=%7B%22terms%22%3A%22mercedes%22%7D+&pxLimit=50
In the example above the search endpoint is used. All parameters in the search endpoint are optional.
Optional parameters, as well as all parameters in 'POST' and 'PUT' endpoints shall be provided via the params array. How these parameters are ultimately sent to the endpoint is a matter of the httpMethod used. Continuing with the example above:
{ expand: { terms: "mercedes" }, pxLimit: 50 }
This will be converted to a URL query string, and appended to the final request URL for 'GET' and 'DELETE' requests. For 'POST' and 'PUT' requests, the object is transformed into a JSON string and sent with a "application/json" content type and submitted to the endpoint as part of the request.
To upload a file to an endpoint that supports it, use the 'file' parameter. This should be the corresponding $_FILE entry when a file is uploaded. Only one file upload per request is supported.
throws | |
---|---|
string
HTTP verb to use (GET, DELETE, POST, PUT)
string
URI to request
array
Array of URI parameters to append to the final URL
array
Data to transmit as url encoded values
mixed
Array with file information. This is the corresponding $_FILE entry when a file is uploaded
array
encodeURIComponent(string $str) : string
Encodes special characters in addition to , / ? : @ & = + $ #
string
The URI component to encode
string
encodeMultipartRequest(string $delimiter, string $field, array $file, array $postFields = array()) : string
Create a multipart/form-data type request from a file and other posted fields.
throws | |
---|---|
string
Delimiter to use in the request
string
Name of the HTML field
array
Array with file information
array
Other posted parameters
string
multipart/form-data request string
api