Create your own custom Google search using PHP

0
954

Google Custom Search is a service that allows website owners to add a customized search engine to their website. With Google Custom Search, users can search for content within a specific website or group of websites.

Website owners can create a Custom Search Engine by defining a list of websites to search, creating a search box and search results page that match their website’s look and feel, and then integrating the search box into their website. The search results are generated by Google’s search technology, but are filtered to only include results from the specified websites.

Google Custom Search also provides an API that allows developers to access search results programmatically and integrate them into their own applications. This API is subject to usage limits and requires an API key to use.

Overall, Google Custom Search is a useful tool for website owners who want to provide a customized search experience for their users, and for developers who want to integrate search results into their own applications.

Follow this video for complete guidance :

Step 1

First, you need to enable the Google Custom Search API and obtain an API key. Go to the Google Developers Console (https://console.developers.google.com/), create a new project, and enable the Custom Search API. Then, create a new API key.

Step 2

In your PHP code, use the following code to make a request to the Google Custom Search API:

// Set your API key and search engine ID
$api_key = 'your_api_key';
$search_engine_id = 'your_search_engine_id';

// Set the search query
$search_query = 'your_search_query';

// Set the API endpoint URL
$api_endpoint_url = "https://www.googleapis.com/customsearch/v1?key={$api_key}&cx={$search_engine_id}&q={$search_query}";

// Make a request to the API and get the response
$response_json = file_get_contents($api_endpoint_url);
$response = json_decode($response_json, true);

Step 3

You can now use the $response variable to access the search results. The $response variable is an associative array that contains the search results in the items key. Here’s an example of how to display the search results:

foreach ($response['items'] as $result) {
    echo '<h3><a href="' . $result['link'] . '">' . $result['title'] . '</a></h3>';
    echo '<p>' . $result['snippet'] . '</p>';
}

This code will display the title and snippet of each search result as a heading and paragraph, respectively.

ALSO READ  Free JSON API for News and Blog Articles

Note that the Google Custom Search API has usage limits and may require payment for high-volume usage. Be sure to check the API documentation for more information on usage limits and pricing.

Complete Source Code

index.php

<!DOCTYPE html>
<html>
    <head>
        <title>Google Search</title>
        <style>
            body {
              background-color: #f2f2f2;
              font-family: Arial, sans-serif;
            }
            #search-container {
              margin: 100px auto;
              width: 500px;
              text-align: center;
            }
            #logo {
            	margin-bottom: 30px;
            }
            #search-box {
              width: 80%;
              padding: 10px;
              font-size: 16px;
              border: none;
              border-radius: 20px;
              box-shadow: 0px 0px 5px #ccc;
              outline: none;
            }
            #search-button, #feeling-lucky-button {
              margin-top: 20px;
              padding: 10px 20px;
              background-color: #fff;
              border: 1px solid #ccc;
              border-radius: 20px;
              box-shadow: 0px 0px 5px #ccc;
              cursor: pointer;
              outline: none;
            }
            #search-button:hover, #feeling-lucky-button:hover {
            	background-color: #f2f2f2;
            }
        </style>
    </head>
    <body>
        <div id="search-container">
            <img src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" alt="Google logo" id="logo">
            <form action="search.php" method="GET">
                <input type="text" name="q" id="search-box" placeholder="Search Google" required>
                <button type="submit" id="search-button">Google Search</button>
            </form>
        </div>
    </body>
</html>

search.php

<?php

// Set your API key and search engine ID
$api_key = 'API_KEY';
$search_engine_id = 'SEARCH_ENGINE_ID';

// Set the search query
$search_query = urlencode($_GET['q']);

// Set the API endpoint URL
$api_endpoint_url = "https://www.googleapis.com/customsearch/v1?key={$api_key}&cx={$search_engine_id}&q={$search_query}";

// Make a request to the API and get the response
$response_json = file_get_contents($api_endpoint_url);
$response = json_decode($response_json, true);

?>
<!DOCTYPE html>
<html>
  <head>
    <title>Google Search Results</title>
    <style>
      body {
  background-color: #f2f2f2;
  font-family: Arial, sans-serif;
}

#header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
}

#logo {
  height: 50px;
}

#search-box {
  width: 80%;
  padding: 10px;
  font-size: 16px;
  border: none;
  border-radius: 20px;
  box-shadow: 0px 0px 5px #ccc;
  outline: none;
}

#search-button, #feeling-lucky-button {
  margin-left: 10px;
  padding: 10px 20px;
  background-color: #fff;
  border: 1px solid #ccc;
  border-radius: 20px;
  box-shadow: 0px 0px 5px #ccc;
  cursor: pointer;
  outline: none;
}

#search-button:hover, #feeling-lucky-button:hover {
  background-color: #f2f2f2;
}

#results-container {
  margin: 20px;
}

.result {
  border-bottom: 1px solid #ddd;
  margin: 20px 0px;
}

.result h3 {
  margin-bottom: 5px;
}

.result h3 a {
  color: #1a0dab;
  text-decoration: none;
}

.result h3 a:hover {
  text-decoration: underline;
}

.result a {
  color: #006621;
  text-decoration: none;
  font-size: 14px;
}

.result a:hover {
  text-decoration: underline;
}


.result p {
  margin-top: 5px;
  font-size: 14px;
}

    </style>
  </head>
  <body>
    <div id="header">
      <a href="index.php"><img src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" alt="Google logo" id="logo"></a>
      <form action="" method="GET">
        <input type="text" style="margin-bottom: 10px;" name="q" id="search-box" placeholder="Search Google">
        <button type="submit" id="search-button">Google Search</button>
      </form>
    </div>
    <div id="results-container">
    	<?php foreach ($response['items'] as $result) {?>
        <div class="result">
          <h3><a href="<?php echo $result['link'];?>"><?php echo $result['title'];?></a></h3>
          <a href=""><?php echo $result['displayLink'];?></a>
          <p><?php echo $result['snippet'];?></p>
        </div>
    <?php } ?>
      
    </div>
  </body>
</html>

 

ALSO READ  Free Sending Emails with PHP

 

Tagsgoogle

Comments are closed.