Php CURL Tutorial – Step by Step for Beginners

In this Php CURL Tutorial you will learn how to create a simple CURL request, a GET CURL request and a POST CURL request with Php in a Step by Step manner. You will also learn to handle errors in CURL requests with Php.

Php CURL is basically for requesting a particular website url for fetching it’s content within our Php script or for transferring some content such as form data to a particular webpage url or a third party API.

So in Php when we use CURL, the Php server uses ‘libcurl’ library under the hood which is developed by Daniel Stenberg.

With Php CURL we can achieve following things:

  • Transfer or Fetch data between servers seamlessly.
  • Transfer or Fetch data between Third party APIs i.e. JSON, XML or Strings etc.
  • Fetch a complete webpage HTML for scraping.

and the possibilities are endless.

In this Php CURL Tutorial you will learn:

  1. How to send a simple CURL request in Php (basics)
  2. GET CURL request in Php (fetching data)
  3. POST CURL request in Php (transfering data)
  4. Error Handling in Php CURL

 

So without wasting anytime lets begin this Php CURL Tutorial…

 

1. How to send a simple CURL request in Php

Step 1: Initialize the CURL Object

First thing first, you need to initialise a CURL object that will create a single curl session for your request. You can do it like that:

//1st step
$curl = curl_init();

Here $curl holds the curl object and curl_init(); function creates that object for you and returns it that you have stored in the variable named $curl

Step 2: Set the website url for your CURL Request

Now that you have the curl object, you need to define the curl object option in which you can put your website url that you need to request.

curl_setopt($curl, CURLOPT_URL,"http://www.someurl.com");

Here curl_setopt() function is to set the curl object options which takes 3 parameters/arguments. You will use this function always to set the curl object options. $curl is your curl object, CURLOPT_URL is the option name and the request url in double quotes.

Step 3: Set the HTTP Header for your CURL Request

You need to set the HTTP Header option for your CURL Request.

curl_setopt($curl, CURLOPT_HTTPHEADER, FALSE);

Here CURLOPT_HTTPHEADER is the option name and we set the header sent to be FALSE. Means for now we are not sending any request header for the website url. Some website urls need the request headers to be defined and sent properly but in this case we can ignore the headers.

Step 4: Set the option to return the webpage HTML

You need to set this option to receive the webpage HTML after the request has been sent successfully and store in a variable.

curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

Here CURLOPT_RETURNTRANSFER is the option which you need to set as TRUE.

Step 5: Execute the CURL Request

After you set all options then you can call the function to execute the CURL request like that

$response = curl_exec($curl);

Here curl_exec(); is the function to execute CURL request and $response is to store the response of the request.

Now the response can contain these:

  • Data i.e. Webpage HTML, JSON, XML as a string if you have defined CURLOPT_RETURNTRANSFER to TRUE
  • TRUE only if you have defined CURLOPT_RETURNTRANSFER to FALSE
  • FALSE only if the CURL request failed

Step 6: Close the CURL Request Session

You always need to close the curl request object to free up server resources like that

curl_close ($curl);

Here curl_close(); function is simply to close the curl request object.

Putting it all together

So if we sum up all the code it will be like that

//step 1
$curl = curl_init();

//step 2
curl_setopt($curl, CURLOPT_URL,"http://www.someurl.com");

//step 3
curl_setopt($curl, CURLOPT_HTTPHEADER, FALSE);

//step 4
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

//step 5
$response = curl_exec($curl);

//step 6
curl_close ($curl);

2. GET CURL request in Php

All the curl requests are GET requests by default so if you want to add parameters for your GET request then you can do so by appending them in query string string like that

curl_setopt($curl, CURLOPT_URL,"http://www.someurl.com?param1=value&param2=value");

Here param1=value and param2=value are the parameters for the GET request appended in the query string with a ? question mark.

Putting it all together

So if we sum up all the code it will be like that

//step 1
$curl = curl_init();

//step 2
curl_setopt($curl, CURLOPT_URL,"http://www.someurl.com?param1=value&param2=value");

//step 3
curl_setopt($curl, CURLOPT_HTTPHEADER, FALSE);

//step 4
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

//step 5
$response = curl_exec($curl);

//step 6
curl_close ($curl);

3. POST CURL request in Php

You can send a POST CURL request by defining the POST request options in your request settings before executing your request like that

curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,array('param1'=>'value', 'param2'=>'value'));

Here CURLOPT_POST is set to 1 to make the request as POST and CURLOPT_POSTFIELDS need to be set with the array of values in the key => value pattern for the data you want to POST (transfer) to the request url.

Putting it all together

So if we sum up all the code it will be like that

//step 1
$curl = curl_init();

//step 2
curl_setopt($curl, CURLOPT_URL,"http://www.someurl.com");

//step 3
curl_setopt($curl, CURLOPT_HTTPHEADER, FALSE);

//step 4
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

//step 5 - Enable POST
curl_setopt($curl, CURLOPT_POST, 1);

//step 6 - Setting POST data
curl_setopt($curl, CURLOPT_POSTFIELDS,array('param1'=>'value', 'param2'=>'value'));
//step 7
$response = curl_exec($curl);

//step 8
curl_close ($curl);

4. Error Handling in Php CURL

If you want to check whether your curl request is successful or not you can do it by using some curl functions for error handling defined like that

if(!$response){
 echo "Error : ".curl_error($curl)." , Code : ".curl_errno($curl);
 exit;
}

Here curl_error(); function shows the error message as a string and curl_errno(); function shows the error number for the type of error.

 

Conclusion

So I hope I am able to put it easy for you in this Php CURL Tutorial and the possible ways you can use it in your applications and projects. In case of any confusions feel free to ask in comments.
If you liked this Php CURL Tutorial then kindly share the word and don’t forget to check out our Youtube and Facebook. Because we will be posting more useful code stuff with tutorial videos so you will always be updated with them.

Have a great day! 🙂

9 Shares

Leave a Reply

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