<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">

<head>
<?php
    $appID 
'YOUR_APP_ID_HERE';

    
$sites = array(
        
'allrecipes.com',
        
'bbcgoodfood.com',
        
'cuisine.com.au',
        
'foodnetwork.com',
        
'food.yahoo.com',
        
'simplyrecipes.com'
    
);

    
/*******************************************************************/

    
$title 'Recipe Search';
    
$description 'Find tasty recipes online with this free and easy to use recipe search engine. Hungry?';
    
$tags 'recipe,search,food,online,search engine';
    
$robots 'index,follow';

    
$search $_GET['search'];
    if (
$search) {
        
$search_printable htmlspecialchars($search);
        
$search_string preg_replace('/\s+/''+'$search);

        
$title 'Recipe Search results for ' $search_printable;
        
$description '';
        
$tags '';
        
$robots 'noindex,follow';
    }
    
$offset $_GET['offset'];
    if (!
$offset) {
        
$offset 0;
    }

    
?>
    <title><?php echo $title?></title>
    <meta name="description" content="<?php echo $description?>"/>
    <meta name="tags" content="<?php echo $tags?>"/>
    <meta name="robots" content="<?php echo $robots?>"/>
</head>
<body>
    <h1><?php echo $title?></h1>

    <!-- Display the search form -->
    <form method="GET">
        <p>
            <label for="search">Search for</label>
            <input id="search" name="search" type="text" value="<?php echo $search_printable?>" />
            <input type="submit" value="Search" />
        </p>
    </form>

    <!-- Do we have some search results? -->
    <?php 
    
if ($search) {
        
$url "http://boss.yahooapis.com/ysearch/web/v1/$search_string+recipe?sites="
              
implode(','$sites) . "&appid=$appID&format=json&start=$offset&count=10";
        
$json file_get_contents($url);
        
$obj json_decode($json);

        if (
$obj->ysearchresponse->responsecode != 200) {
            
// An error occurred
            
echo <<<QQ
                <div class="error">
                <h2>An Error Occurred!</h2>
                <p>Sorry, we&rsquo;ll do our best to get this fixed as soon as possible!</p>
                </div>
QQ;
        } else {
            if (
$obj->ysearchresponse->count === 0) {
                
// No results were found
                
echo <<<QQ
                <h2>No Results Found</h2>
                <p>Sorry, we couldn&rsquo;t find any results for &ldquo;
$search_printable&rdquo;. Try refining your search for a broader match.</p>
QQ;
            } else {
                
// Display all the results
                
foreach($obj->ysearchresponse->resultset_web as $result) {
?>
                <div class="search-result">
                <h2><a href="<?php echo $result->url?>" title=""><?php echo $result->title?></a></h2>
                <p class="abstract"><?php echo $result->abstract?></p>
<p class="url"><a href="<?php echo $result->url?>" title=""><?php echo $result->url?>
                </a></p>
                </div>
                <?php }
                
?>
                <p class="next-prev">
                <?php
                
if ($offset >= 10) {
                    
$prev_offset $offset 10;
                    
$prev $_SERVER["SCRIPT_NAME"] . "?search=$search_string&offset=$prev_offset";
                    
?>
                    <a href="<?php echo $prev?>" title="Previous results">Previous</a>
                    <?php
                
}
                if (
$obj->ysearchresponse->count == 10) {
                    
$next_offset $offset 10;
                    
$next $_SERVER["SCRIPT_NAME"] . "?search=$search_string&offset=$next_offset";
                    
?>
                    <a href="<?php echo $next?>" title="More results">Next</a>
                    <?php
                
}
                
?></p><?php
            
}
        }
    } 
?>
</body>
</html>