-
Notifications
You must be signed in to change notification settings - Fork 29
Request
SugarAPI\SDK\Request\
SugarAPI\SDK\Request\Interfaces\RequestInterface
SugarAPI\SDK\Request\Abstracts\AbstractRequest
This Abstract Request implementation, has some default CURL Options configured for the CURL Resource. The following options care set on the CURL Resource for most implementations unless configured otherwise.
| Option | Value | Description |
|---|---|---|
CURLOPT_HTTP_VERSION |
CURL_HTTP_VERSION_1_0 | |
CURLOPT_HEADER |
TRUE | Include the Header in the response body |
CURLOPT_SSL_VERIFYPEER |
FALSE | Turn off SSL Certificate Verification, so that self generated SSL Certs can work with REST PHP Client and to ensure compatibility with most platforms. |
CURLOPT_RETURNTRANSFER |
TRUE | Tells Curl to return the Response on execution |
CURLOPT_USERAGENT |
Sugar-REST-PHP-Client | The User Agent on each request |
The following method descriptions align with the abstract implementation on the AbstractRequest Class.
Set the HTTP Method the Request object will use
| Name | Type | Description |
|---|---|---|
$type |
string | The HTTP Request Method (GET,POST,PUT,DELETE etc.) |
| Type | Description |
|---|---|
| self | The current Request Object for method chaining |
Get the HTTP Method of the Request object
| Type | Description |
|---|---|
| string | The HTTP Request Method |
Set the Body to Request
| Name | Type | Description |
|---|---|---|
$body |
mixed | The request body/payload that is to be submitted by the request |
| Type | Description |
|---|---|
| self | The current Request Object for method chaining |
Get the Body on the request
| Type | Description |
|---|---|
| mixed | The body of the request |
Add a Header to the Request Headers property, doesn't set the CURL Headers Option until Sending
| Name | Type | Description |
|---|---|---|
$name |
string | The name of the HTTP Header |
$value |
string | The value of the HTTP Header |
| Type | Description |
|---|---|
| self | The current Request Object for method chaining |
Sets the Headers on the Curl Request object, called during Sending. Appends to Request Headers property using the above addHeader() method
| Name | Type | Description |
|---|---|---|
$headers |
array | An associative array containing the Headers to be set on the Curl resource |
| Type | Description |
|---|---|
| self | The current Request Object for method chaining |
Get the Headers configured on the Request Object
| Type | Description |
|---|---|
| array | The array of Headers on the Request |
Retrieve the CURL resource being used by the Request Object
| Type | Description |
|---|---|
| resource | The CURL resource |
Set an Option on the Curl Resource. Stores array of options on the 'options' property on the Object as well
| Name | Type | Description |
|---|---|---|
$option |
string | A CURL Option to be configured on the CURL resource |
$value |
string | The value for the CURL Option |
| Type | Description |
|---|---|
| self | The current Request Object for method chaining |
Get the list of Options set on the Curl Resource
| Type | Description |
|---|---|
| array | The options configured on the CURL Resource |
| @return array |
Set the URL on the Request Object
| Name | Type | Description |
|---|---|---|
$url |
string | The URL of where the request will be sent |
| Type | Description |
|---|---|
| self | The current Request Object for method chaining |
Get the URL configured on the Request Object
| Type | Description |
|---|---|
| string | The URL configured on the Request Object |
Execute the Curl Request. Before sending, Headers are added to the Curl Object, based on the Headers array configured on the Request Object
| Type | Description |
|---|---|
| self | The current Request Object for method chaining |
Get the raw CURL Response Object generated by the CURL resource
| Type | Description |
|---|---|
| CURL Response Resource | The response received by CURL resource |
Initialize Curl Resource, called during constructor of Request Object
| Type | Description |
|---|---|
| self | The current Request Object for method chaining |
Close the Curl Resource
| Type | Description |
|---|---|
| self | The current Request Object for method chaining |
Close and Restart the Curl Resource
| Type | Description |
|---|---|
| self | The current Request Object for method chaining |
Get the Status of the Curl Object. Statuses are Initialized, Sent or Closed
| Type | Description |
|---|---|
| string | Returns the current status of the CURL resource |
There are six (6) implementations of the Request Object that represent the most common request methods for REST APIs
| Name | Description |
|---|---|
SugarAPI\SDK\Request\GET |
A class tailored to the HTTP GET Request implementation, specifically for JSON data |
SugarAPI\SDK\Request\POST |
A class tailored to the HTTP POST Request implementation, specifically for JSON data |
SugarAPI\SDK\Request\PUT |
A class tailored to the HTTP PUT Request implementation, specifically for JSON data |
SugarAPI\SDK\Request\DELETE |
A class tailored to the HTTP DELETE Request implementation, specifically for JSON data |
SugarAPI\SDK\Request\GETFile |
A class tailored to retrieving a File via a GET Request. Extends the GET class listed above, but removes the default headers configured on the request |
SugarAPI\SDK\Request\POSTFile |
A class tailored to submitting a File via a POST Request. Extends the POST class listed above, but alters the Headers for multipart/form-data to be submitted |
The above Request Objects are utilized by the provided Endpoint Objects. Typically when working with the REST PHP Client, usage of the Request Object will be limited, as the Endpoint Object should be managing it for most scenarios.
However certain situations arise, that might cause of direct usage of the Request Objects. Some common scenarios that arise are listed below.
By default, the GET, POST, PUT and DELETE implementations have a the "Content-Type: application/json" header configured on the Request.
When using a custom Endpoint or Custom client, you may need to alter the headers on the Request. The following code outlines how to alter the Headers:
//When working with an Endpoint Object
$Endpoint = $Client->createRecord('Accounts');
$Request = $Endpoint->getRequest();
//Add the header to the Request Object's header property
$Request->addHeader('custom-header','true');
//Set the Headers on the CURL Resource
$Request->setHeaders();
The above code, calls setHeaders() after adding the new Header to the Request Object. This isn't exactly necessary, since the setHeaders() method is called when send() is called on the Request. It was mainly added to the code snippet to show the difference between the two methods.