The Web Scraping API allows the developer to scrape data from the website in a structured format. It returns realtime data from the websites based on the web page URL specified in the API settings. The Web Scraping API is very useful when you want to extract content from the HTML source of the web pages.
There are various Web Scraping API available to scrape the webpage data, Scrapestack is one of the best free Web Scraping API among them. Scrapestack API enables you to scrape data from the website in realtime. Scrapestack provides easy-to-use REST API that extracts data from a website without any programming and restriction with IP blocks, CAPTCHA, or geolocations. In this tutorial, we will show you how to integrate Web Scraping API with Scrapestack REST API using PHP.
Follow the below simple steps to integrate Web Scraping API with scrapestack in PHP.
1. Before getting started, create an account on scrapestack.
2. In the dashboard, you will get the API key under the Your API Access Key.
The Access Key is required to authenticate and access the scrapestack API.
http_build_query()
function to pass required params in the scrapestack API.access_key
parameter.url
parameter.$queryString = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY',
'url' => 'https://www.google.com',
]);
To scrape content from the website, call Web Scraping API via HTTP GET request using cURL in PHP.
// API URL with query string
$apiURL = sprintf('%s?%s', 'http://api.scrapestack.com/scrape', $queryString);
// Create a new cURL resource
$ch = curl_init();
// Set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute and get response from API
$website_content = curl_exec($ch);
// Close cURL resource
curl_close($ch);
HTTPS Encryption:
To make secure API requests use HTTPS (SSL) encryption by calling API URL begins with https
.
https://api.scrapestack.com/scrape
After a successful API request, the webpage content will be returned in a structured format.
// Render website content
echo $website_content;
The following are the complete code to extract webpage content using PHP.
<?php
$queryString = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY',
'url' => 'https://www.google.com',
]);
// API URL with query string
$apiURL = sprintf('%s?%s', 'http://api.scrapestack.com/scrape', $queryString);
// Create a new cURL resource
$ch = curl_init();
// Set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute and get response from API
$website_content = curl_exec($ch);
// Close cURL resource
curl_close($ch);
// Render website content
echo $website_content;
?>
The scrapestack API is free to use, there also premium plans are available for advanced uses. In the example code, we have used some required parameters for Web Scraping API call. Various configuration options are available in scrapestack API, you can use these to customize the scraping data. For a complete reference, see the documentation of scrapestack API.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
thanks for sharing your valuable knowledge about web scraping .