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 '{"username":"password","password":"password"}' 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 '{"title":"test post titile","description":"The test post desc"}'

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 '{"id":2, "title":"updated title","description":"updated desc"}'

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