• logo
  • PHP
  • PHP OOPs
  • script
    • JavaScript
    • JQuery
    • Ajax
    • AngularJs
    • VueJs
    • NodeJs
    • ReactJs
  • wordpress
  • Codeigniter
  • Codeigniter 4
  • Laravel
  • Python
  • MySql
  • Json
  • C
  • C++
  • More ...
    • Cakephp Framework
    • MongoDb
    • MySqli PDO
    • .htaccess
    • HTML 5
    • CSS
    • SEO
    • DCA

Codeigniter 4 Tutorials

  • Codeigniter 4
  • Application Run
  • Application Structure
  • Configuration files
  • Errors
  • Controller
  • View
  • Model
  • How to remove index.php from URL
  • Route
  • CodeIgniter URLs
  • Helper
  • Session
  • Global Functions and Constants
  • Logging Information in CI4
  • Error Handling
  • Signin / SignUp
  • Login / Register
  • CRUD
  • Crud 1
  • Form Validation
  • Data Table
  • Pagination
  • File Upload
  • Show Data Using Ajax
  • Select2 AJAX Autocomplete Search
  • Curl Post
  • Rest API
  • Weblink
Home » Codeigniter 4  » 

How to Send PHP cURL POST Request in Codeigniter 4?


controller

File name : index.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class CurlController extends CI_Controller {

public function __construct() {
parent::__construct();
}

public function curPostRequest()
{
/* Endpoint */
$url = 'http://www.localhost.com/endpoint';

/* eCurl */
$curl = curl_init($url);

/* Data */
$data = [
'name'=>'John Doe',
'email'=>'johndoe@yahoo.com'
];

/* Set JSON data to POST */
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);
}
}

Example :- the header authentication cURL example

File name : index.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class CurlController extends CI_Controller {

public function __construct() {
parent::__construct();
}

public function curPostRequest()
{
/* Endpoint */
$url = 'http://www.localhost.com/endpoint';

/* eCurl */
$curl = curl_init($url);

/* Data */
$data = [
'name'=>'John Doe',
'email'=>'johndoe@yahoo.com'
];

/* Set JSON data to POST */
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);
}
}

File name : index.php


File name : index.php


File name : index.php


File name : index.php


File name : index.php


File name : index.php


File name : index.php





Itechtuto

Connect Us Socially:

Quick Links

  • itech
  • About Us
  • Feedback
  • Trademarks
  • Privacy Policy
  • Terms of Use
  • Sitemap
  • Trademarks
  • Privacy Policy
  • Terms of Use
  • Sitemap

Copyright © 2016 itechxpert (P) Ltd. All rights reserved.

Copyright © 2016 itechxpert (P) Ltd. All rights reserved.