0

I am using php to extract data from google search results. Here is my code in php.

<?php
$url="https://www.google.co.in/search?      
q=allintext:Theatre%20Actors&sort=date&cr=countryIN&aqs=chrome..69i57.1033j0j7&sourceid=chrome&es_sm=93";
  $homepage = file_get_contents($url);
  $ans = htmlentities($homepage);
  $doc = new DOMDocument();
  @$doc->loadHTML($homepage);
  $tags = $doc->getElementsByTagName('h3');
  $finder = new DOMXPath($doc);
  $node = $finder->query("//h3[contains(@class, 'r')]");
  foreach ($node as $tag) {
   $temp = $tag->getElementsByTagName('a');               
   $string = "";
   foreach ($temp as $key) {
        $k=0;
        $data = $key->getAttribute('href');
        //echo $data;
        for($i=0;$i<strlen($data);$i++){
            if($data[$i]=="&") break;
            if($data[$i]=="="){
                $k=1; continue;
            }
            if($k==1){
                 $string.=$data[$i];   
            }
        }
   }
   $idx=  split(":", $string);
   if(strcmp($idx[0],"http")!=0 && strcmp($idx[0],'https')!=0)     continue; 
   $ans = '<a href='.$string.'>'.$string.'</a>';
   echo $ans;
   echo '<hr>';
  }
  ?>

After I run this script, I get this error: Warning: file_get_contents(https://www.google.co.in/search?q=allintext:Theatre%20Actors&sort=date&cr=countryIN&aqs=chrome..69i57.1033j0j7&sourceid=chrome&es_sm=93): failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable in /opt/lampp/htdocs/search/script.php on line 4

Please help . Thanks In Advance.

wadhwasahil
  • 468
  • 7
  • 28

1 Answers1

2

Maybe Google blocks your request: - if your are running the script too often - google itself dont like bots like 'file_get_contents'

see also: HTTP request failed! HTTP/1.1 503 Service Temporarily Unavailable

Community
  • 1
  • 1