In this post, I’ll explain how to do a simple web page extraction in PHP using cURL, the ‘Client URL library’.

The curl  is a part of libcurl, a library that allows you to connect to servers with many different types of protocols. It supports the http, https and other protocols. This way of getting data from web is more stable with header/cookie/errors process rather than using simple file_get_contents(). If curl() is not installed, you can read here for Win or here for Linux.

Setting Up cURL

First, we need to initiate the cURL handle:

Then, set CURLOPT_RETURNTRANSFER to TRUE to return the transfer page as a string rather than put it out directly:

Executing the Request & Checking for Errors

Now, start the request and perform an error check:

Closing the Connection

To close the connection, type the following:

Extracting Only the Needed Part and Printing It

After we have the page content, we may extract only the needed code snippet, under id=”case_textlist”:

The Whole Scraper Listing

This sample will guide you and give you further practice in daily web scraping.