How to Integrate Rest API Using cURL PHP -Tutorials

Rest API using cURL PHP

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.

1.How to integrate POST REST API using cURL PHP.

Send the post request Example

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.

The Header section needs to specify the Content-Type and Content-Lenght by comma separated.

Decode the JSON data and store in a variable

we can perform further activity once we receive the server response as 200.

Integrate Rest API using cURL PHP

2. How to get data from Rest API using curl in PHP.

Use the below curl code to fetch the data through GET REST API using using curl php

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.

How to get data from Rest API using curl in PHP

3. How to update record using PUT Rest API using curl in PHP.

Use the below code to update your record through PUT API.