Zulhaiman Mohd Nasir
Zulhaiman Mohd Nasir

Reputation: 53

How to apply cUrl get data in CodeIgniter

I would like to ask about applying cUrl on CodeIgniter.
Where should I put the cURL coding, is it in view or controller?

Upvotes: 4

Views: 8522

Answers (2)

Syahmi Roslan
Syahmi Roslan

Reputation: 110

In the controller would be fine, for example

`function index()
{
    // Get cURL resource
    $curl = curl_init();
    // Set some options - we are passing in a useragent too here
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_URL => 'http://localhost/ci_paginationv2-
        master/index.php/API/API_airtime_share',
        CURLOPT_USERAGENT => 'Codular Sample cURL Request'
    ));
    // Send the request & save response to $resp
    $resp = curl_exec($curl);
    // Close request to clear up some resources
    curl_close($curl);

    echo $resp;

}` 

Upvotes: 1

Ashwani Goyal
Ashwani Goyal

Reputation: 616

Best practice would be putting your logic in the controller itself as per the Code Igniter Conventions and then process the response there itself (if needed) and then render the view for the same (if the data needs to be viewed). Please comment if something is unclear.

Click Here to see more on this

Upvotes: 0

Related Questions