Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
Return the Link of first result in google search in PHP

I want to return the first URL of google search result like: First Url Result

Google search

.php code:

<?php
        //Check if submit button is clicked or not
        if (isset($_POST['submit'])) {
            $text = $_POST['text'];
            // echo $text;

            function file_get_contents_curl($url) {
                $ch = curl_init();
            
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
                curl_setopt($ch, CURLOPT_URL, $url);
            
                $data = curl_exec($ch);
                curl_close($ch);
            
                return $data;
            }
            
            $query =  $text;
            $url = 'http://www.google.co.in/search?q='.urlencode($query).'';
            echo $url;
            $scrape = file_get_contents_curl($url);
            echo $scrape;
?>

How can i achieve that?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

By default since using this scraping method only sends an HTTP request and does not run render page as browsers do, google won't load you the page you are looking for and it will show you an agreement page like this. enter image description here

You should use the google Search API as mentioned by ADyson in the comment

Another approach which is not recommended is that you use selenium or any headless browser. Using the headless browser you will still be prompt by agreement but behind it you can scrape the search results. enter image description here

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!