Google Search Results Scrap Using PHP Library

Want to scrap google search results using PHP is very popular method. Yes you can scrap or fetch google search results by below method..

Google Search Results Scraping Using PHP Library

I have covered everything within this article, by end of this article you will understand how to scrap google search results using PHP.





Requirements:

You need 

1) Simple HTMl DOM Parser

2) cURL enabled PHP Server.

3) Final PHP Coding.


So lets get started.

1) You can download this DOM Parser PHP library here: https://sourceforge.net/projects/simplehtmldom/

After download you can install with composer or simply can integrate using require function and Use/Traits call. simply as below.


require 'vendor/simplehtmldom/simple_html_dom.php'; // this file location in the root directory of your project folder.

use 'simplehtmldom/HtmlWeb';


Check image this is how i have added to my php project to opencart store.


Check in final code below.


2) Make sure to check if your server is enabled for cURL, you can simply check in php info page.

You u do not have php info page then create one php page on your server. Like php_info.php

and paste this code. 

<?php echo $phpinfo(); ?>

And run this page on browser and find cURL where is says enabled. If not then enable from your server. Ask your server provider to enable it.


3) Now coding time.

Create a php page like results-data.php


<?php

require 'vendor/simplehtmldom/simple_html_dom.php';

use simplehtmldom\HtmlWeb;

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/search?q=php+tutorial&gl=us&hl=en');

    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.4951.54 Safari/537.36');

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

 

Comments :

Add your valuable comments, so others can read.

Leave A Comment :