Bridgeline Digital Logo
Menu

Rendering API - Off Platform cURL Example

Use of the RenderingAPI with cURL and PHP

The example below uses cURL and PHP to perform a rendering of content that is served from OrchestraCMS. For available parameters and options, please see the Rendering API documentation. In using the originId renderType, the content layout(s) must be one of those available in the current published versions of that content.

$url = 'https://www.example.com/services/apexrest/cms/ocmsrest/'; // the URL for your live site
$fields = array(
    'service'                         => 'OrchestraRenderingAPI',
    'apiVersion'                      => '5.0',
    'action'                          => 'getRenderedContent',
    'sname'                           => 'my_site_name',
    'application'                     => 'runtime',
    'renderingRequest' => array(
        'parameters' => array(
            'renderType' => 'originId'
        ),
        'listParameters' => array(
            'contentLayouts' => array(
                'my_content_layouts',
            ),
            'originIds' => array(
                'my_version_origin_ids',
            ),
        ),
    ),
);

$fields_string = '';

// Required to convert the array of POST parameters into the correct URL
foreach ($fields as $key => $value) {
    if (!is_array($value)) {
        $fields_string .= $key . '=' . $value . '&';
    } else {
        $fields_string .= $key . '=' . json_encode($value) . '&';
    }
}

$fields_string = rtrim($fields_string, '&');

$ch = curl_init(); // Initiate a cURL function
curl_setopt($ch, CURLOPT_URL, $url); // Set the URL to be used
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Determine whether to verify the authenticity of the peer's certificate
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Required to return the response for use in your page
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);

// If rendering content from an Intranet, please provide a valid Salesforce Session ID and uncomment the below 3 lines
//$token = 'SF_SESSION_ID';
//$authorization = 'Authorization: Bearer ' . $token; // Prepare the authorization token
//curl_setopt($ch, CURLOPT_HTTPHEADER, array($authorization)); // Inject the token into the header

$result = curl_exec($ch); // Execute the cURL request
curl_close($ch); // Close the cURL request

$obj = json_decode($result);
$renderings = json_decode($obj->responseObject);

foreach ($renderings as $rendering) {
    if (is_array($rendering)) {
        foreach ($rendering as $render) {
            foreach ($render->renderMap as $renderMap) {
                echo htmlspecialchars($renderMap); // htmlspecialchars is used for demonstration purposes, in order to render the returned HTML markup as plain text. This will likely need to be omitted in production environments
            }
        }
    }
}