REST API Integration Using cURL PHP. CURL stands for Client URL Library, The cURL PHP is an open-source curl lib. The curl PHP lib support multiple protocols like HTTP, HTTPS, FTP, FTPS, SMTP, etc. The cURL PHP also supported the SSL certificate. cURL PHP allows communicating multiple servers with different protocols like HTTP, HTTPS, etc.
Visit the Oficial website for more information.
Now in this tutorial we can learn how to integrate HTTP REST POST, GET, PUT, and Delete API using cURL PHP.
$data = array(
'FirstName' => $firstName,
'LastName' => $lastName,
'UserName' => $email,
'UserType' => $usrtype,
'phone_number' => $contactNo,
'password' => $password,
'Address' => $address
);
$data_string = json_encode( $data );
$url = "YOUR_API_URL";
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_string );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen( $data_string ) ) );
$response = curl_exec( $ch );
$obj = ( array )json_decode( $response, true );
$httpcode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
In the above code, you can store the form post value in array example $data = array(‘FirstName’ => $firstName) with comma separated. And store data in json fomat by using json_encode( $data ); function.
To start the new cURL session we can use the curl_init( $url ) function to pass the REST API URL. curl _init() take optional arguments.
Set curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ) as true to store Json response in variable.
curl_setopt($ch, CURLOPT_POST, true) function send the data to the REST URL.
curl_setopt( $ch, CURLOPT_POST, true );
The Header section needs to specify the Content-Type and Content-Lenght by comma separated.
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen( $data_string ) ) );
Decode the JSON data and store in a variable
$obj = ( array )json_decode( $response, true );
we can perform further activity once we receive the server response as 200.
$httpcode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
//Result 200 Success
// Error 404, 500
Use the below curl code to fetch the data through GET REST API using using curl php
$endPointURL = "YOUR_API_URL";
$curl = curl_init();
curl_setopt_array( $curl, array(
CURLOPT_URL => $endPointURL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
) );
$ListResponse = curl_exec( $curl );
$resultSt = ( array )json_decode( $ListResponse );
$allList = ( object )$resultSt;
foreach ( $allList as $bList ) {
$ID= (array)($bList ->_id)
}
You will get the API response in JSON format. After getting the response from the API. We can use json_decode () to convert the JSON string to a PHP object and stored in the array variable.
Use the below code to update your record through PUT API.
$data = array(
'storeName' => $storeName,
'offDays' => $offdays,
'openTime' => $openTime,
'closeTime' => $closeTime,
'description' => $description
);
$data_string = json_encode( $data );
$url = "YOUR_API_URL";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer ' . $authToken,
'Content-Length: ' . strlen( $data_string ) ) );
// SET Method as a PUT
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'PUT' );
// Pass user data in POST command
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_string );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
// Execute curl and assign returned data
$response = curl_exec( $ch );
// Close curl
$obj = json_decode( $response );
$httpcode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );