How to use cURL in PHP, curl in php example, php curl api, php curl post example, php curl extension, curl php post, curl php, get php curl library, php curl header

How to use cURL in PHP

cURL is a PHP library that helps the web developer send files and also download data over HTTP and FTP and allows you to work with data that is not present in your database, that is, it gives you access to data that is outside your server space. cURL makes it easy to communicate between different websites and domains as it provides the developer with an option to post data to a specified URL and get the result instantly.

cURL stands for the client URL where URL is spelled as uppercase to make it obvious that it deals with URLs. It is pronounced as ‘see URL’.

In PHP, the most basic use of cURL is

  • To initialize or start a curl session
  • To set different options or parameters that it will use
  • To execute functions which may include sending/posting data to the server or getting/fetching data from the server
  • To close or terminate the cURL session.

While using cURL you will come across these functions

  1. curl_init() which is used to start your cURL session
  2. curl_setopt() which is used to set different options for your cURL request
  3. curl_exec() which executes the functions you have defined above
  4. curl_close() which is used to close the session once it has been executed successfully.

Mostly, using the cURL in your projects, the curl_setopt function is the one you will mostly deal with, as it allows you to initialize various CURLOPT_* request options. The curl_setopt function takes three arguments:

  1. a cURL handle,
  2. the CURLOPT_XXX option
  3. the value of the CURLOPT_XXX option.

A basic example of how the structure of cURL looks like is as shown below

<?php  
//define the url where you will post/fetch data to/from
$url="https://www.solutionspace.com/apis/example"
// to create a cURL resource
$ch = curl_init() ;
//set cURL options
curl_setopt($ch, CURLOPT_URL, $url) ;
//Run cURL (execute http request)
curl_exec($ch) ;
// close cURL resource

curl_close($ch) ;
//display result
echo $result ;
?>

 

CURLOPT_URL option is used to set the request URL to your desired URL where you are either fetching or sending data with the curl_setopt function

In the example structure that we have defined above, you may have noted that we have only included a URL without posting any data. To post data to a different server, see the example below

<?php
$url = ' https://www.solutionspace.com/apis/example';
$curl = curl_init();
$data = array(
'value1_placeholder' => 'value1',
'value2_placeholder' => 'value2'
);
$data_string = http_build_query($data);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
$result = curl_exec($curl);
curl_close($curl);
echo $result;
?>

We submit the data above using the HTTP POST method which works the same as the POST method in a form

The $data variable holds the form values which in this case use value_placeholder which should be exactly as how the recipient of data or the owner of API is receiving data as and mostly it is collected and stored in an array.

The http_build_query function is used to prepare a URL-encoded query string

The CURLOPT_POST option is set to TRUE so as to allow the request method to work as an HTTP POST

The form data is posted by using the CURLOPT_POSTFIELDS option.

Remember you can also submit the post data using the json_encode method other than as an HTTP build query but this one will be determined by the recipient of the data how he has structured his API so you will need to read the documentation provided.

An example of how to submit data as json_encoded is as below

<?php
$url = ' https://www.solutionspace.com/apis/example';
$curl = curl_init();
$data = array(
'value1_placeholder' => 'value1',
'value2_placeholder' => 'value2'
);
$data_string = json_encode($data);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($curl);
curl_close($curl);
echo $result;
?>

 

The examples we have outlined above show how you can use cURL to either fetch data from a different server or post data to a different server and receive an instant response which you can use in your application. Please note the basics that cURL must have so that it can process your request successfully.