Tips & Tutorials

Search via API

As of clearFusionCMS 1.3.0 we expose an API, one of the features of the API is that you can perform searches on remote installs of the CMS.

The following snippet can be placed on an existing search results page to search a remote install and display the top 3 results.

$request = $tplEngine->controller->getRequest();
if(($q = $request->getQuery('q')) && !$request->hasQuery('page')) {
    
    $resultTpl = isset($resultTpl) ? $tplEngine->getChunk($resultTpl) : '<li><a href="[[+link]]">[[+title]]

[[+summary]]

'; $wrapperTpl = isset($wrapperTpl) ? $tplEngine->getChunk($wrapperTpl) : '<ul>[[+results]]'; $client = new flHttpClient('http://example.com/manager/api/1/search/query/'); $client->setAuth('your API key', '') ->setQueryData(array( 'q' => $q, 'pageSize' => 3 )); if($client->sendRequest(flHttpClient::GET) && $client->getResponseCode() == 200) { $r = json_decode($client->getBody()); if($r->totalMatches) { echo "<h2>Results from the remote install"; echo '<p>' . $r->totalMatches . ' results found, <a href="http://example.com/search-results/?q=' . urlencode($q) . '">Search again on remote site?

'; $html = ''; foreach($r->results as $result) { $html .= $tplEngine->parseString($resultTpl, array( 'link' => $result->link, 'title' => $result->title, 'summary' => $result->summary )); } echo $tplEngine->parseString($wrapperTpl, array('results' => $html)); } } }

example.com must be replaced with the address of the site to run the search on.

'your API key' must be replaced with a valid API key from the installation receiving the search request.

You can see this feature in use on http://clearfusioncms.com/ as when you search it also performs the search on the user guide hosted at http://docs.clearfusioncms.com/