- Curl Rest API Example Using PHP
- How to enable cURL in PHP
- PHP 7.4 curl install ubuntu
- Simple cURL login request
- GET Request Using Curl
- GET Request Using Curl With Header
- Force Curl To Get a Response in JSON format
- POST Request Using Curl
- PUT Request Using Curl
- Delete Request Using Curl
- A simple example of REST API CURL with PHP
- How to use CURL via a proxy
- PHP : REST API – GET data using cURL
- What is cURL?
- How to use cURL in PHP
- Sample REST API data in JSON
- PHP program to GET REST API data using cURL
- Code Explanation
Curl Rest API Example Using PHP
This tutorial help to execute GET, POST, PUT, HEAD, DELETE HTTP Requests against a REST API using cURL.curl is a command-line tool for transferring data using various protocols.
The CURL supports FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, SMTP, SMTPS, Telnet, TFTP and more.
curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form-based upload, proxies, HTTP/2, cookies, user+password authentication (Basic, Plain, Digest, CRAM-MD5, NTLM, Negotiate and Kerberos), file transfer resume, proxy tunneling and more.
How to enable cURL in PHP
The cURL, by default, is not enabled in Apache. If we try to run CURL programs without enabling CURL in Apache, the browser will throw an error like below.
Fatal error: Call to undefined function curl_init()
You can avoid the above error, we need to enable the CURL extension in the Apache server by following below steps:
- Step 1: Locate PHP.ini file, it is mostly in the server’s root folder or public_html then open the PHP.ini in a text editor.
- Step 2: Search or find the ;extension=php_curl.dll with Ctrl+F and remove the semi-colon ‘;’ before it to activate it.
- Step 3: Save and Close PHP.ini with Ctrl+S and restart Apache from terminal/CMD
PHP 7.4 curl install ubuntu
You can install Curl in ubuntu by following the below steps:
This command installs the PHP CURL.
sudo apt-get install php5-curl
This command starts with the Apache server.
sudo service apache2 restart
Simple cURL login request
Below is the sample curl request that helps to create a login:
curl -k -X POST -H 'Content-Type: application/json' -d '' http://localhost:8080/get_token
GET Request Using Curl
The GET is a very common request that used to fetch data from the api.
curl https://localhost:8080/posts
GET Request Using Curl With Header
if you want to include a header in a curl request then you need to pass ‘-i’ params with curl.
curl -i https://localhost:8080/posts
Force Curl To Get a Response in JSON format
Sometimes, We will get the rest API response in XML format but we can set ‘—header «Accept:application/json»‘ option in curl to force curl to get a response in JSON format.
curl --header "Accept:application/json" url
POST Request Using Curl
below is the PHP post curl request
curl -i -X POST -H "Content-Type:application/json" http://localhost:8080/createpost/ -d ''
PUT Request Using Curl
I am using a PUT type curl request to update data in a database. I am passing update parameters in JSON format and passed within the CURL request.
$ curl -v -H "Content-Type:application/json" -X PUT http://localhost:8080/updatepost/2 -d ''
Delete Request Using Curl
We will use a DELETE type CURL request to delete data from the database using rest API. I will pass the post id with the rest API URL like we are passing ‘2’ .
curl -v -X DELETE http://localhost:8080/deletepost/2
A simple example of REST API CURL with PHP
You can use curl to create a REST API wrapper on the server-side that will call third-party rest API and return a response to your application. I am using PHP programming language to call the rest API call but you can use it as per your need.
// create curl resource $ch = curl_init(); // set url curl_setopt($ch, CURLOPT_URL, "http://graph.facebook.com/me"); //return the transfer as a string curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // $output contains the output string $output = curl_exec($ch); print_r($output); // close curl resource to free up system resources curl_close($ch); ?>
How to use CURL via a proxy
Below is a working example curl with the proxy setting.
$url = 'http://php-file-url'; $proxy = '127.0.0.1:8888'; //$proxyauth = 'user:password'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_PROXY, $proxy); //curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1); $curl_scraped_page = curl_exec($ch); curl_close($ch); echo $curl_scraped_page;
I have added CURLOPT_PROXYUSERPWD in case any of your proxies require a username and password.
To disable the proxy simply set it to null .
curl_setopt($ch, CURLOPT_PROXY, null);
You can control proxy HTTP and HTTPS settings using the below configuration parameter:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // false for https
PHP : REST API – GET data using cURL
In this tutorial, we will learn how to GET API data using cURL functions.
What is cURL?
cURL (stands for client URL) is a command-line tool to transfer data between servers. It supports various protocols such as HTTP, HTTPS, FTP, FTPS, IMAP etc. Most programming languages support cURL commands using some libraries or extension.
cURL is one of the popular methods to interact with REST API data. PHP use cURL library (module) to provide access curl functions within PHP. By default, most PHP installations come with cURL support is enabled.
How to use cURL in PHP
The basic process of PHP cURL can be divided into four parts.
- Initialize a cURL session using $curl_handle = curl_init();
- Pass URL option in the cURL session curl_setopt( $curl_handle, CURLOPT_URL, $url );
- Execute cURL session & store data: $api_data = curl_exec($handle) ;
- Close cURL session: curl_close( $curl_handle );
Sample REST API data in JSON
We will use Dummy REST API Example website to work with live API data in our examples. This website provides a number of routes or URLs to read, create, update, delete employee’s data using API. We will use following to get employees data.
Above URL provides employee’s profile data in JSON format something similar to following:
PHP program to GET REST API data using cURL
In the following PHP program, we will use a sample PHP CURL function to get employee’s data and displays them.
data; // Extract only first 5 user data (or 5 array elements) $user_data = array_slice($user_data, 0, 4); // Traverse array and print employee data foreach ($user_data as $user) < echo "name: ".$user->employee_name; echo "
"; > ?>
Code Explanation
First, we need to initiate a curl and pass your API URL. Then execute CURL & store received data. As API URL returns JSON data, we need to decode it using json_decode. Now you have all data as an array which you can traverse using foreach and display accordingly.
In the next chapters, we will use cURL methods to perform an update, create and delete operations using API data.