Integrate with Funnelback using PHP

Background

This article provides code for a simple PHP wrapper that can be used to integrate with Funnelback.

This enables the nesting of search results within PHP generated page.

Code example

This code is an extremely simple PHP wrapper example. It demonstrates a minimal viable implementation to request Funnelback results from the server side in a PHP environment. It should not be considered viable for production.

Please note that this example does not include cookie support and - as such - will not be usable in conjunction with Funnelback search sessions.

<html>
    <head>
    </head>
    <body>
         <?php
           // The base components for our request
           $protocol = "http://";
           $host = "search.myserver.co.uk";
           $base_path = "/s/search.html?";
           // Don't forget to input your collection name below
           $default_cgi_string = "collection=YOUR_COLLECTION_HERE&query=";
           $url = $protocol . $host . $base_path;
           // Do we have a CGI string? This is an important check!
           if ($_GET) {
                // Rebuild our query string - do not filter the CGI params
                $url .= http_build_query($_GET);
           } else {
                // More importantly, we set the default cgi string
                // if none is given.
                $url .= $default_cgi_string;
           }
           // Now let's init our cURL to fetch the results
           $curl = curl_init();
           curl_setopt_array($curl, array(
                   CURLOPT_RETURNTRANSFER => 1,
                   CURLOPT_URL => $url,
                   CURLOPT_TIMEOUT => 5,
                   // PHP has a quirk that changes periods into underscores. The 'f.' period is used widely in Funnelback for faceting and has to be adjusted before sending the CURL request.
                   CURLOPT_URL => str_replace("f_","f.",$url)
           ));
           // Execute
           $response = curl_exec($curl);
           // and close.
           curl_close($curl);
           if($response){
                // The response here contains the rendered HTML results
                echo $response;
           } else {
                // You should take appropriate action here
                echo "<p>Something went wrong</p>";
           }
           ?>

    </body>