0

Hello I am having trouble finding a way to detect a string exists on Google's Search. So if I give the PHP file:

This is plagiarized

It would tell me, to test you can put your query in Google like so: "This is plagiarized", and if it is it would have some results but, if it is original it comes back with no results.

Book Of Zeus
  • 49,509
  • 18
  • 174
  • 171
StratHaxxs
  • 19
  • 9
  • 1
    So, what are you asking? – Federkun Nov 23 '15 at 22:59
  • I am asking to find a way to tell if there are results to a string in google using php @Federico – StratHaxxs Nov 23 '15 at 23:02
  • You can get contents from https://www.google.com.ua/#safe=off&q=This+is+plagiarized with file_get_contents if i understand correctly your question – Sam Ivichuk Nov 23 '15 at 23:03
  • @SamIvichuk How would I tell it it has results or not? – StratHaxxs Nov 23 '15 at 23:04
  • @StratHaxxs you should parse the result (with simplehtmldom for example) and see if there are any rows. Do I need to post code? – Sam Ivichuk Nov 23 '15 at 23:07
  • @SamIvichuk Please post the code. :) – StratHaxxs Nov 23 '15 at 23:08
  • So let me get this straight. You have a file and you want to know whether `This is plagiarized` sentence exists in the file or not, correct? – Rajdeep Paul Nov 23 '15 at 23:10
  • 1
    Scraping Google search results like this is not a good approach. http://stackoverflow.com/questions/22657548/is-it-ok-to-scrape-data-from-google-results – Don't Panic Nov 23 '15 at 23:13
  • @Don'tPanic Oh. Ok Thanks for telling me! :) – StratHaxxs Nov 23 '15 at 23:15
  • Your question is very unclear and doesn't show *what you have done so far*. Usually, adding some code to the question allows other people to give an appropriate answer that suits your approach or suggest improvements to it. No code at all means that every possible answer makes sense hence not a single answer is a good one. – Frederik.L Nov 24 '15 at 01:20

2 Answers2

1

Google has its own api. You can read the documentation here.

An example with the deprecated api:

$search = "some unique contents";

$opts = array(
    'http'=>array(
        'method'=>"GET",
        'header'=>"Referer: http://your_site.com\r\n" // see the terms of service
    )
);

$context = stream_context_create($opts);
if (false !== $data = file_get_contents('http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="'.urlencode($search).'"', false, $context)) {
    $json = json_decode($data, true);
    if (!empty($json['responseData']['results'])) {
        echo "This is plagiarized!";
    }
}

Note that the query its wrappet between ". That's tell google to find the exact phrase.

Federkun
  • 36,084
  • 8
  • 78
  • 90
1

Better to use Federico's answer, but if it doesn't work for some reason, here is another approach:

    $str = "color";
    $html = file_get_contents("https://www.google.com.ua/search?q=".urlencode($str));

    if (strpos($html,"id=\"resultStats\"></div>") === false) {
        echo "yes";
    }
    else {
        echo "no";
    }
Sam Ivichuk
  • 999
  • 1
  • 10
  • 22