Skip to content
Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

Youths Forum Youths Forum Youths Forum

Tech Blogs & Programming Tutorials

  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones

Categories

  • Automobiles (12)
    • Cars (7)
  • Blog (103)
    • Poems (2)
    • Space (2)
  • Command (2)
  • Education (2)
  • Entertainment (4)
  • Gadgets (9)
    • Phones (8)
      • Android Phones (4)
  • HTML Templates (11)
  • IT Training Institutes (1)
  • Lifestyle (4)
  • News (51)
  • Others (23)
  • Programming (296)
    • API (16)
    • CSS (83)
    • Database (4)
    • Hosting (1)
    • HTML (37)
    • JavaScript (117)
      • JQuery (27)
      • ReactJS (7)
    • PHP (116)
  • Python (3)
  • recipes (1)
  • SEE Result (1)
  • Server (3)
  • Blog
  • News
  • Programming
    • PHP
    • JavaScript
    • JQuery
    • CSS
    • HTML
    • API
  • Stock Market Live
  • Automobiles
    • Cars
  • Gadgets
    • Phones
    • Android Phones
Close

Search

PHP

Create your own custom Google search using PHP

By Admin
March 15, 2023 4 Min Read
Comments Off on Create your own custom Google search using PHP

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.

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>

 

 

Tags:

google
Author

Admin

Follow Me
Other Articles
Previous

PHP script for creating a Social Media Sharing System

Next

Google Trends Unleashed: A Comprehensive Guide to Search Data Analysis

FIFA World Cup 2026 Predict and Win by SportsGuff

Recent Posts

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide
  • Chiranjibi Adhikari Appointed Acting President of CAN Federation
  • How to Design a Student Marksheet Using HTML and CSS

Tags

adsense ai animate animation animation using HTML and CSS API blog calculator chatgpt Cryptocurrency CSS css animation design Email Facebook featured filemanager file manager free template google htaccess HTML image Instagram interview javascript JQuery jquery ui NADA AutoShow NADA Auto Show 2024 password PHP Progressive Web App PWA QR random react reactjs Rotate travel Twitter vpn youthforum youthsforum youtube

About Us

At Youths Forum, we are passionate about sharing knowledge that empowers students, educators, professionals, and technology enthusiasts.

Our Mission

Our mission is simple: to make technology and education accessible, understandable, and beneficial for everyone. We strive to create content that helps our readers learn new skills and stay updated with industry developments.

RSS RSS

  • Unpacking Nepal’s Record Rs 2.12 Trillion Budget and What It Means for You Admin
  • How to Write a Strong Scholarship Application: The Ultimate Step-by-Step Guide Admin
  • How to Prepare for Exams Without Stress: The Ultimate Science-Backed Guide Admin

Quick Links

  • Stock Market Live
  • Parliament Election 2082
Copyright 2026 — Youths Forum. All rights reserved. Blogsy WordPress Theme