如何在CodeIgniter中应用cUrl获取数据

时间:2022-06-08 07:48:29

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

我想问一下在CodeIgniter上应用cUrl。我应该在哪里放置cURL编码,是在视图还是控制器?

2 个解决方案

#1


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;

}` 

#2


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.

最佳实践是根据Code Igniter Conventions将您的逻辑放在控制器本身,然后在那里处理响应(如果需要),然后渲染相同的视图(如果需要查看数据)。如果不清楚,请评论。

Click Here to see more on this

点击这里查看更多相关信息

#1


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;

}` 

#2


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.

最佳实践是根据Code Igniter Conventions将您的逻辑放在控制器本身,然后在那里处理响应(如果需要),然后渲染相同的视图(如果需要查看数据)。如果不清楚,请评论。

Click Here to see more on this

点击这里查看更多相关信息