Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Search via API
07-22-2013, 10:59 PM
Post: #1
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.
PHP Code:
$request $tplEngine->controller->getRequest();
if((
$q $request->getQuery('q')) && !$request->hasQuery('page')) {
    
    
$resultTpl = isset($resultTpl) ? $tplEngine->getChunk($resultTpl) : '<li><a href="[[+link]]">[[+title]]</a><p>[[+summary]]</p></li>';
    
$wrapperTpl = isset($wrapperTpl) ? $tplEngine->getChunk($wrapperTpl) : '<ul>[[+results]]</ul>';
    
    
$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</h2>";
            echo 
'<p>' $r->totalMatches ' results found, <a href="http://example.com/search-results/?q=' urlencode($q) . '">Search again on remote site</a>?</p>';
        
            
$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/

clearFusionCMS Lead Developer
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)