Bing Site Search in PHP with Pagination

11 06 2011
This is a simple yet powerful way to get Bing's Site Search API working on your site in PHP. Simply update the $config variables below and you'll be good to go.



<?php
define('TITLE', 'Search Results');
include('header.php');

$config['appid'] = "01234567890"; // change this
$config['search_domains'] = array("domain1.com", "domain2.com");
$config['results_per_page'] = 20;

$url = "http://$_SERVER[HTTP_HOST]$_SERVER[PHP_SELF]";
?>

<?php echo (isset($_REQUEST['q']) ? "<h1>Search Results</h1>" : "<h1>Search</h1>"); ?>
<form method="get">
    <div style="text-align:right">
        <input type="text" id="q" name="q" value="<?php echo (isset($_REQUEST['q']) ? $_REQUEST['q'] : 'Search...'); ?>"/>
        <input type="submit" value="Search" name="submit" id="searchButton" />
    </div>
</form>

<?php
if (isset($_REQUEST ['q'])) {
    $query = htmlentities($_REQUEST ['q']);
    if (strlen($query) < 1)
        echo "You didn't type anything!";
    else {
        if (isset($_REQUEST ['page'])) {
            $page = htmlentities($_REQUEST ['page']);
            $offset = (($page - 1) * $config['results_per_page']);
        } else
            $offset = 0;

        $result = "http://api.search.live.net/json.aspx?Appid={$config['appid']}&sources=web";
        $result .= "&query=" . urlencode("site:" . implode(" OR site:", $config['search_domains']) . " " . $query);
        $result .= "&Web.Count={$config['results_per_page']}&Web.Offset=$offset";
        //echo $result;
        $result = file_get_contents($result);
        $result = json_decode($result);
        echo('<ul ID="resultList">');
        foreach ($result->SearchResponse->Web->Results as $value) {
            echo('<li class="resultlistitem"><a href="' . $value->Url . '">');
            echo('<h3>' . $value->Title . '</h3></a>');
            echo('<p>' . $value->Description . '</p>');
        }
        echo("</ul>");




//        echo("<pre>");
//        print_r($result);
//        echo("</pre>");
        $results = $result->SearchResponse->Web->Total > 1000 ? 1000 : $result->SearchResponse->Web->Total;
        if ($results > 0)
            echo $results . " results <br />\n";
        else
            echo "Sorry, no results to display. <br />\n";

        $pages = (int) ($results / $config['results_per_page']);
        // echo "$pages pages <br />\n";
        // pagination
//        echo "Page #s:<br />";
        for ($i = 1; $i <= $pages; $i++) {
            echo "<a href=$url?q=$query&page=$i>$i</a>  ";
        }
    }
}
?>

<?php include('footer.php'); ?>

 


Trackbacks


No Trackbacks

Comments

Display comments as (Linear | Threaded)
No comments

Add Comment


You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
BBCode format allowed

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA