Click here to Skip to main content
16,020,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I want to get google search results in a div using ajax. It works fine but when I get the results it reduces the width card-body.

1. I want to ask is there any way to prevent it.
2. is there any way to get the results only and not the google logo etc.

Thanks.


Google_Search.php

PHP
<?php 
$q = $_POST['q'];
$url = 'https://www.google.com/search?q=' . $q;
$response = file_get_contents($url);
echo $response;
?>


What I have tried:

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Search Web</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script> 
</head>
<body>
<div class="row d-flex justify-content-center">
    <div class="card shadow-lg mb-4 rounded">
        <div class="card-header p-3 bg-success text-white">Search Results</div>
        <div class="card-body">
        <form action="" method="post">
            <input type="text" placeholder="Searh Web" name="q" id="q" class="form-control m-2">
            <div class="d-grid gap-2">
            <button type="submit" id="btnSearch" class="btn btn-primary">Search Web</button>
            </div>
        </form>
            <div class="row" id="search_result">

            </div>
        </div>
    </div>
</div>
<script>
    $(function(){
        $("#btnSearch").click(function(e){
            e.preventDefault();
            var search = $("#q").val();
            $.ajax({
                type: "post",
                url: "google_search.php",
                data: {q:search},
                success:function(response)
                {
                    $("#search_result").html(response);
                }
            })
        })
    })
</script>
</body>
</html>
Posted

1 solution

Google offers a search API library to accomplish what you are trying to do. Implementing it will be much more complex than what you are currently trying.

To put it another way, what you get for "free" from Google is formatted HTML including their logo etc. If you want the underlying unformatted data, you will need to register for an API key with Google, and depending on how you are using the results and with what volume, there may be a monetary cost to use this service.

This article explains the process in more detail. Google Search API: Everything You Need To Know[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900