Skip to Content
PHPCODE
scrape data from url php
php code / September 13, 2021

The Web Scraping API enables a developer to extract structured data from a website. Based on the web page URL supplied in the API settings, it retrieves real-time data from websites. When you need to extract content from the HTML source of online sites, the Web Scraping API comes in handy.

There are several Web Scraping APIs accessible for scraping webpage data, with Scrapestack being one of the top free Web Scraping APIs available. You can scrape data from a website in real time using the Scrapestack API. Scrapestack is a simple REST API that pulls data from a website without requiring any programming knowledge and without being restricted by IP bans, CAPTCHA, or geolocations. We’ll teach you how to use PHP to combine the Web Scraping API with the Scrapestack REST API.

To integrate Web Scraping API with Scrapestack in PHP, follow the simple steps below.

1. Obtain an API Access Key Create an account on Scrapestack before you begin.

2. The API key can be found in the dashboard’s Your API Access Key section.

 serp-api-access-key

Configuring the API
To authenticate and use the scrapestack API, you’ll need your Access Key.

To pass needed params via the scrapestack API, generate the query string using the http build query() function.
In the access key parameter, enter the API Access Key.
In the url argument, type the URL of the webpage.

$queryString = http_build_query([ 
'access_key' => 'YOUR_ACCESS_KEY', 
'url' => 'https://www.google.com', 
]);

Send an HTTP GET request.
To scrape content from a website, use cURL in PHP to make an HTTP GET request to the Web Scraping API.

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

Use HTTPS (SSL) encryption to make secure API queries by contacting API URLs that begin with https.

https://api.scrapestack.com/scrape

Scraping Content from Websites
The content of the webpage will be delivered in a structured fashion after a successful API request.

// Render website content 
echo $website_content;

Scrape Content from a Website Using Scrapestack API Example
The following is the whole PHP code for extracting webpage content.

<?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; 
?>

Note:

The scrapestack API is free to use, however subscription plans for more sophisticated uses are available. We utilised certain needed parameters for the Web Scraping API request in the example code. Scrapestack API has a number of configuration options that you may use to personalise the scraping data. See the scrapestack API documentation for a detailed reference.

PHPCODE © 2024