CURL in PHP Tutorial basics

I have been asked this question multiple times from my colleagues and my fellow-mates . What is curl? How to use curl ? and When to use Curl ? So in the following blog I am going to answer all your questions .

In PHP if you want to access outside URL from your website there are four ways to do so
Using the  fopen() function
Using the  fsockopen() function
Using the  cURL library
Using the  Http_Request class

All these function have their own specific use but cURL is the most recommended one.
In order to run cURL in your website or in php you need to install and enable it . If it is already installed you can skip to part for basic use of curl .

How to Verifying that cURL is installed in your system
So, Before you want to go and run curl functions, You need to check it is enabled or not on you server . So Create a test.php file with the following code:

Access this file from your browser and search “CURL support” if you find something this means curl is installed and your can skip to Basic Use of Curl

Installing cURL in PHP

Installation of curl in php is quite simple , just find and open to your php.ini file and look for this line:

Then remove the semi-colon (;) from the beginning of the line, save the php.ini file and then restart your server, and Thats It! curl is installed in your system .

Basic use of cURL in PHP

Well, first of all, let the simplest script you can use to get the response of a site (in this case, is the new.txt file here’s blog):

After running this code, the contents of the new.txt will be present in the $result variable . Now you can get the HTML  of any website using this method.

The function curl_setopt() give a lot of options to use in curl , You can look into the documentation of curl for all the list of options here http://php.net/manual/en/book.curl.php.

Checking if the website is accessible and online using cURL
With this script you can access public addresses and URLs and find our if it returns 404 ( Page Not Found ) error or not.

As you can see we added a new option which is  CURLOPT_FOLLOWLOCATION, this allows the CURL to follow all redirects in the URL so that it reaches the final URL.

Sending data to forms using cURL (POST method)
If you want to use cURL for sending data to a form, just like when you are typing the data and submitting a  form. For this you need 2 things : a list of  fields and the action of the form (that is where the data will be sent) :

Some times the website on which we are working needs a referrer url which is used for authentication for most of the websites . So for this scenario we can use the following script .

But suppose you tested the script and saw that something went wrong .. And after doing your homework, found that the site allows only coming from the actual site data (ie, it checks the REFERRER which is the page address in which the data was entered). So you set your script as follows:

There are many more possibilities to use curl and many more options to use curl so you have learned about basics of curl but for advancement you have to explore more . Hope you enjoyed it .

Leave a Reply

Your email address will not be published. Required fields are marked *