How to get 10000+ SERPs data using PHP
Hello friends, in today’s article, we are going to tell you how you can scrape or get Google search engine or other search engine results. And in this you can also extract the result of all the pages. With loop in php code, let us know how you can extract these results.
Method 1
To get data from Google search results, you can use the Google Search API. Here’s an example of how you might use the API with PHP to get search results for a specific query:
- First, you’ll need to sign up for a Google API key and enable the Custom Search API:
- Go to the Google Cloud Console
- Click the project drop-down and create a new project
- Click the hamburger menu and select APIs & Services > Library
- Search for “Custom Search API” and click on the result
- Click “Enable”
- Next, you’ll need to create a Custom Search Engine that searches the entire web:
- Go to Google Custom Search
- Click the “Add” button to create a new search engine
- In the “Sites to search” field, enter “*” (without the quotes) to search the entire web
- Click “Create”
- Once you have your API key and Custom Search Engine ID, you can use PHP to make requests to the API. Here’s an example of how you might do this using the Guzzle HTTP client library:
<?php
require 'vendor/autoload.php';
$client = new GuzzleHttp\Client();
$apiKey = "YOUR_API_KEY";
$cseId = "YOUR_CSE_ID";
$query = "YOUR_QUERY";
$response = $client->get("https://www.googleapis.com/customsearch/v1", [
"query" => [
"key" => $apiKey,
"cx" => $cseId,
"q" => $query,
],
]);
$results = json_decode($response->getBody(), true);
foreach ($results["items"] as $result) {
echo $result["title"] . "\n";
echo $result["link"] . "\n";
echo $result["snippet"] . "\n";
}
This example makes a GET request to the API with the specified API key, search engine ID, and query. The API returns a JSON object containing the search results, which the example code then parses and loops through to print the title, link, and snippet of each result.
Note that the Google Search API has a limit of 100 search results per query, so you’ll need to use the start
parameter to paginate through the results if you want to retrieve more than 100 results.
Also Read: Advantages and disadvantages of Kedarnath ride on Bike.
I hope this helps! Let me know if you have any questions or need further assistance.
Method 2
To get a large amount of search engine results page (SERP) data using PHP, you can use the Google Search API or the Bing Web Search API. These APIs allow you to request the top search results for a given query and receive the results in a structured format, such as JSON.
To use the Google Search API, you will need to sign up for a Google Cloud account and obtain an API key. You can then use the API key to make HTTP requests to the Google Search API using PHP. Here’s an example of how to make a request to the Google Search API using PHP’s built-in file_get_contents
function:
Also Read: Advantages of Red bananas
$apiKey = 'YOUR_API_KEY';
$query = 'YOUR_QUERY';
$url = 'https://www.googleapis.com/customsearch/v1?key=' . $apiKey . '&cx=017576662512468239146:omuauf_lfve&q=' . urlencode($query);
$results = json_decode(file_get_contents($url), true);
print_r($results);
This will make a request to the Google Search API and return the top search results for the specified query in a JSON format, which you can then process and extract the data you need.
To use the Bing Web Search API, you will need to sign up for a Microsoft Azure account and obtain an API key. You can then use the API key to make HTTP requests to the Bing Web Search API using PHP. Here’s an example of how to make a request to the Bing Web Search API using PHP’s built-in file_get_contents
function:
$apiKey = 'YOUR_API_KEY';
$query = 'YOUR_QUERY';
$url = 'https://api.cognitive.microsoft.com/bing/v7.0/search?q=' . urlencode($query);
$options = [
'http' => [
'header' => "Ocp-Apim-Subscription-Key: $apiKey\r\n",
],
];
$context = stream_context_create($options);
$results = json_decode(file_get_contents($url, false, $context), true);
print_r($results);
This will make a request to the Bing Web Search API and return the top search results for the specified query in a JSON format, which you can then process and extract the data you need. I hope this helps! Let me know if you have any questions.
Also Read: How to change the domain name in your WordPress site?
Recommended
Sorry, no related articles to display.