Extract Website Content with Web Scraping API using PHP

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.

Get API Access Key

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.

web-scrape-api-access-key-codexworld

API Configuration

The Access Key is required to authenticate and access the scrapestack API.

  • Build the query string using http_build_query() function to pass required params in the scrapestack API.
  • Specify the API Access Key in the access_key parameter.
  • Specify the webpage URL in the url parameter.
$queryString http_build_query([ 
    
'access_key' => 'YOUR_ACCESS_KEY',
    
'url' => 'https://www.google.com',
]);

Make HTTP GET Request

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($chCURLOPT_URL$apiURL);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

// 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

Scraping Website Content

After a successful API request, the webpage content will be returned in a structured format.

// Render website content 
echo $website_content;

Example Code to Scrape Content from Website via scrapestack API

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($chCURLOPT_URL$apiURL);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);

// Execute and get response from API
$website_content curl_exec($ch);

// Close cURL resource
curl_close($ch);

// Render website content
echo $website_content;

?>

Conclusion

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

1 Comment

  1. Jitendra Sahu Said...

Leave a reply

keyboard_double_arrow_up