In this article, we will explain to you how to send php CURL PUT request in Codeigniter 4(Codeigniter 4 cURL PUT Request Example Tutorial). so we will give you a simple example of codeigniter 4 curl example.
normally this request curl method same as the POST request method but when we need to change or update third party API data that time we use cURL PUT Request.
If you want to convert curl to PHP code then you can go to the URL. this converter easily converts the curl code to PHP code.
The php provides four curl request functions like GET, POST, PUT, DELETE. which is used to connect the frontend to the backend as a third party API or resting API. in this example, we are using PHP cURL PUT request in Codeigniter 4. so you can see our following example.
PHP Codeigniter cURL PUT Request example
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class CurlController extends CI_Controller { public function __construct() { parent::__construct(); } public function curlPutRequest() { /* Endpoint */ $url = 'http://www.localhost.com/endpoint'; /* eCurl */ $curl = curl_init($url); /* Set PUT Request */ curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); /* Data */ $data = [ 'name'=>'John Doe', 'email'=>'johndoe@gmail.com' ]; /* Set JSON data to PUT*/ curl_setopt($curl, CURLOPT_POSTFIELDS, $data); /* Define content type */ curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Content-Type:application/json' )); /* Return json */ curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); /* make request */ $result = curl_exec($curl); /* close curl */ curl_close($curl); } } ?>
PHP Codeigniter cURL PUT Request with header authentication example
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class CurlController extends CI_Controller { public function __construct() { parent::__construct(); } public function curlPutRequest() { /* Endpoint */ $url = 'http://www.localhost.com/endpoint'; /* eCurl */ $curl = curl_init($url); /* Set PUT Request */ curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); /* Data */ $data = [ 'name'=>'John Doe', 'email'=>'johndoe@gmail.com' ]; /* Set JSON data to PUT*/ curl_setopt($curl, CURLOPT_POSTFIELDS, $data); /* Define content type */ curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'Content-Type:application/json', 'App-Key: JJEK8L4', 'App-Secret: 2zqAzq6' )); /* Return json */ curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); /* make request */ $result = curl_exec($curl); /* close curl */ curl_close($curl); } } ?>