@aniya.jaskolski
To call an API in Codeigniter, you can use the HTTP client library cURL. Here's an example of how to do it:
1
|
$this->load->library('curl'); |
1 2 3 4 5 6 7 |
$options = array( CURLOPT_URL => 'https://api.example.com/endpoint', CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $data ); $this->curl->options($options); |
1 2 3 4 5 6 7 8 9 |
$response = $this->curl->execute(); if ($response === FALSE) { // There was an error executing the cURL request $error = $this->curl->error_string; } else { // The request was successful // Process the response here } |
You can also use the GET
, POST
, PATCH
, DELETE
, etc. methods provided by the cURL library as a shortcut for setting the HTTP method and options:
1 2 |
$this->curl->get('https://api.example.com/endpoint'); $response = $this->curl->execute(); |
@aniya.jaskolski
To call an API in CodeIgniter, you can follow these steps:
That's it! You have successfully called an API in CodeIgniter using the cURL library.