In this tutorial, We will learn how to curl for getting requests, delete requests, put requests, etc  in PHP. 


Example:

<?php

/* API URL */
$url = 'http://www.mysite.com/api';
  
/* Init cURL resource */
$ch = curl_init($url);
  
/* Array Parameter Data */
$data = ['name'=>'Ratholrji', 'email'=>'testmail@gmail.com'];
  
/* pass encoded JSON string to the POST fields */
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  
/* set the content type json */
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
  
/* set return type json */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  
/* execute request */
$result = curl_exec($ch);
   
/* close cURL resource */
curl_close($ch);


Example:

Header Auth 

<?php
/* API URL */
$url = 'http://www.mysite.com/api';
   
/* Init cURL resource */
$ch = curl_init($url);
  
/* Array Parameter Data */
$data = ['name'=>'Rathorji', 'email'=>'testmail@gmail.com'];
  
/* pass encoded JSON string to the POST fields */
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  
/* set the content type json */
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type:application/json',
            'App-Key: 123456',
            'App-Secret: 1233'
        ));
  
/* set return type json */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  
/* execute request */
$result = curl_exec($ch);
  
/* close cURL resource */
curl_close($ch);


Thanks, Now you can run your application..