Skip to content
Emmanuel D edited this page Mar 24, 2017 · 3 revisions

Namespace

SugarAPI\SDK\Request\

Interface

SugarAPI\SDK\Request\Interfaces\RequestInterface

Abstracts

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

Methods

The following method descriptions align with the abstract implementation on the AbstractRequest Class.

setType($type);

Set the HTTP Method the Request object will use

Arguments

Name Type Description
$type string The HTTP Request Method (GET,POST,PUT,DELETE etc.)

Returns

Type Description
self The current Request Object for method chaining

getType();

Get the HTTP Method of the Request object

Returns

Type Description
string The HTTP Request Method

setBody($array);

Set the Body to Request

Arguments

Name Type Description
$body mixed The request body/payload that is to be submitted by the request

Returns

Type Description
self The current Request Object for method chaining

getBody();

Get the Body on the request

Returns

Type Description
mixed The body of the request

addHeader($name, $value);

Add a Header to the Request Headers property, doesn't set the CURL Headers Option until Sending

Arguments

Name Type Description
$name string The name of the HTTP Header
$value string The value of the HTTP Header

Returns

Type Description
self The current Request Object for method chaining

setHeaders(array $array);

Sets the Headers on the Curl Request object, called during Sending. Appends to Request Headers property using the above addHeader() method

Arguments

Name Type Description
$headers array An associative array containing the Headers to be set on the Curl resource

Returns

Type Description
self The current Request Object for method chaining

getHeaders();

Get the Headers configured on the Request Object

Returns

Type Description
array The array of Headers on the Request

getCurlObject();

Retrieve the CURL resource being used by the Request Object

Returns

Type Description
resource The CURL resource

setOption($option, $value);

Set an Option on the Curl Resource. Stores array of options on the 'options' property on the Object as well

Arguments

Name Type Description
$option string A CURL Option to be configured on the CURL resource
$value string The value for the CURL Option

Returns

Type Description
self The current Request Object for method chaining

getOptions();

Get the list of Options set on the Curl Resource

Returns

Type Description
array The options configured on the CURL Resource
@return array

setURL($url);

Set the URL on the Request Object

Arguments

Name Type Description
$url string The URL of where the request will be sent

Returns

Type Description
self The current Request Object for method chaining

getURL();

Get the URL configured on the Request Object

Returns

Type Description
string The URL configured on the Request Object

send();

Execute the Curl Request. Before sending, Headers are added to the Curl Object, based on the Headers array configured on the Request Object

Returns

Type Description
self The current Request Object for method chaining

getCurlResponse();

Get the raw CURL Response Object generated by the CURL resource

Returns

Type Description
CURL Response Resource The response received by CURL resource

start();

Initialize Curl Resource, called during constructor of Request Object

Returns

Type Description
self The current Request Object for method chaining

close();

Close the Curl Resource

Returns

Type Description
self The current Request Object for method chaining

reset();

Close and Restart the Curl Resource

Returns

Type Description
self The current Request Object for method chaining

getCurlStatus();

Get the Status of the Curl Object. Statuses are Initialized, Sent or Closed

Returns

Type Description
string Returns the current status of the CURL resource

Implementations

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

Usage

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.

Setting Custom Headers

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.