0

I would like to post string to Google search form and get a link of the first result. Now i have just easy script without any POST and using SimpleHtmlDom.

    $arrContextOptions = [
        'ssl' => [
            'verify_peer' => false,
            'verify_peer_name' => false
        ],
        'http' => [
            'method' => "GET",
            'header' => "Accept-language: en\r\n" .
                "User-Agent:    Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6\r\n".
                "Cookie: foo=bar\r\n"
        ]
    ];
$html = HtmlDomParser::file_get_html("http://www.google.com/search?q=".urlencode($title), false, stream_context_create($arrContextOptions));
$link = $html->find("div[id=ires] div[class=g] h3[class=r] a",0)->href;

but the problem is, that format of this "href" is not in the right format, so i can´t use it. I tried to find some Google API for that, but was not successful.

How can i solve this problem?

crffty
  • 424
  • 1
  • 5
  • 16
kores59
  • 443
  • 1
  • 5
  • 16

1 Answers1

2

You should use Google API to do that. Google doesn't allow user to do this. Even you can do it technically, Google will ban your IP because it violates their policy. I suggest you to use https://github.com/imarc/google-site-search for this purpose.

Anson Wong
  • 1,034
  • 8
  • 10
  • usually they popup a captcha when they detect a bot, not ban you outright (at least not initially) –  May 08 '18 at 22:07
  • Actually they will let bot traffic through initially, but start throwing up captchas when it gets too much. They will never ban your IP because they understand that they change owners frequently. – pguardiario May 09 '18 at 00:23