Skip to main content

Simple Web Scraping with Zenscrape API using PHP

Web scraping or data mining is a way to get the desired data from web pages programmatically. Most of the businesses uses web scraping systems to get the useful data from other websites to use in their businesses.

As a developer we sometimes write a simple script to scrape the data from websites. But it’s not an easy task to write complete script as there are so many limitations to pass the process to scrape the data from other websites.

So if you’re running a business or a developer and looking for a complete solution to scrape the websites data, then you’re here at the right place. In this tutorial, you will learn how to scrape the data from websites with Zenscrape API using PHP.

The Zenscrape API provides an easy solution to scrape the web pages data without having any technical knowledge. You can use their visual web scraper with simple options to scrape the data into your desired data format like CSV, JSON etc. The Zenscrape API is smart enough to rotates each request with different IPs from different countries all over the world and return only the valid results.

Also, read:

So let’s proceed to integrate Zenscrape API with PHP to scrape the data from websites.

Step1: Get API Key

First we will get the Zenscrape API key to access the API. So first we need to sign up for free to create an account and get the API KEY.

We will use the API Key for the authentication purpose to make HTTP request to Zenscrape API.

https://app.zenscrape.com/api/v1/get?apikey=APIKEY

Step2: Create Search Query

The API provides many parameters to pass with the API request to scrape the data. So we will create the search query with the required parameters to scrape the data. We will use url parameter with the website URL from which we want to scrape the data. We can also use other useful parameters such location, render and premium and many more to enhance the process.

<?php
$dataSearchQuery = [
	'url' => 'http://example.com',
	'render' => '1',
	'premium' => '',
	'location' => 'eu',
];
?>

Step3: Make HTTP Request to Zenscrape API

We need to make HTTP request to Zenscrape API to scrape the data. So we will make HTTP request using PHP Curl library and use the $dataSearchQuery data with the request. We also need to pass API Key to authenticate the API request and get the response data.

<?php

$apiAccessKey = 'YOUR_API_KEY';

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);

curl_setopt($ch, CURLOPT_URL, 'https://app.zenscrape.com/api/v1/get?' . http_build_query($dataSearchQuery));

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	"Content-Type: application/json",
	"apikey: $apiAccessKey",
));

$responseData = curl_exec($ch);
curl_close($ch);

?>

Step4: Decode Response Data

We will use the PHP json_decode() function to decode the $responseData data for further use.

<?php

$result = json_decode($responseData);
var_dump($result);

?>

Step5: Complete code

Below is the complete code to scrape the data from websites using Zenscrape API with PHP.

<?php

$apiAccessKey = 'YOUR_API_KEY';

$dataSearchQuery = [
	'url' => 'http://example.com',
	'render' => '1',
	'premium' => '',
	'location' => 'eu',
];

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);

curl_setopt($ch, CURLOPT_URL, 'https://app.zenscrape.com/api/v1/get?' . http_build_query($dataSearchQuery));

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	"Content-Type: application/json",
	"apikey: $apiAccessKey",
));

$responseData = curl_exec($ch);
curl_close($ch);

$result = json_decode($responseData);
var_dump($result);

?>

Step6: Conclusion

In this tutorial you have learned how to integrate the Zenscrape API with PHP to scrape the data from websites. You can also try the visual web scraper from Zenscrape API to scrape the data for free by creating your account. You can also checkout the documentation> for more options and details.

You may also like: