Using cURL in Customer Portal (CP3)

It is possible with most Customer Portal sites to make outbound HTTPS/HTTP calls to external services using the PHP cURL library. CURL is installed and available as a PHP extension that can be dynamically loaded in your scripts.

A Note for "Secure" pod users (PS, PC, DISA)

Outgoing connections from "secure" pod web servers are generally blocked by the firewall. If you want the firewall opened, you have to submit an Incident to Oracle Customer Care and hope that they are in a good mood. They are generally reluctant to open up anything on the Web Server pool. You may have to resort to begging.

Site Version Matters

There was a defect in earlier versions of CP3 that made it difficult to load cURL. See this note from Ernie Turner @ Oracle RightNow CX for more information: http://communities.rightnow.com/posts/4139e9dee8?commentId=35240#35240. It has been reported that there was a creative work around, but this is no longer necessary.

The fix is available in the following older CP3 versions and service packs:

  • Nov 12 - SP5
  • Feb 13 - SP3
  • May 13 - SP2

Loading cURL in a Controller and performing a Google Search using the Google Web Search API

The following code show a custom controller that uses cURL to make a HTTP GET request to the Google Web Search API and query for the keyword "RightNow CX".

After uploading this file to your CP site's cp/customer/development/controller directory as CurlExample.php, the code would be executed in your browser at the following address: https://your_site.custhelp.com/cc/CurlExample/getResults. Hint: be sure you've set your browser to view Customer Portal in "Development Mode".

<?php
namespace Custom\Controllers;

/**
 * Example of using cURL in a controller
 * @author andy@45n.co
 */
class CurlExample extends \RightNow\Controllers\Base
{
    /**
     * Search using Google Search API
     */
    public function getResults()
    {
        //Load CURL .so file
        \load_curl();

        //Note: GoogleAPI's are available even on Secure Pods.
        //Typically outgoing HTTP connections are blocked on "secure" pods unless the firewall is open to that host
        $url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=RightNow%20CX";

        //Do cURL stuff
        $ch = \curl_init();
        \curl_setopt($ch, CURLOPT_URL, $url);
        \curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $body = \curl_exec($ch);
        \curl_close($ch);

        // Process the response as JSON
        $json = \json_decode($body);
        
        //Display the Data
        if($json)
        {
            echo "<h1>Results</h1>";
            foreach($json->responseData->results as $searchResult)
            {
                echo $searchResult->title . ": " . $searchResult->url . "<br/>\n\n";
            }
        }
    }
}

The results of accessing the controller will look something like this:

CURL is loaded by simply calling a global function named load_curl().


public function getResults()
{
    //Load CURL .so file
    \load_curl();
    ...
}

This function will work inside of all Customer Portal files including Controllers, Models, Widgets, and Views (not recommended due to MVC separation). Once cURL is loaded you have access to all of the curl_* functions that are normally available. From here you can use cURL directly to make HTTP/HTTPS calls, or use it in conjunction with another library such as nuSOAP to make SOAP calls.

For more information about the PHP cURL library see: https://php.net/curl

Categories : 

Zircon - This is a contributing Drupal Theme
Design by WeebPal.