douben7493 2017-03-14 14:13
浏览 90
已采纳

使用cURL在CodeIgniter项目中调用控制器

Okay so this problem is bothering me quite a bit.

I've created a controller function in my CodeIgniter projects at [project_url]/admin/orderpicking/get_updated_statuses

function name: get_updated_statuses controller name: Orderpicking.php controller location: application/admin

Inside the controller all I have is

ob_start();
print_r('success');
file_put_contents('file.txt', ob_get_contents());
ob_end_clean();

to confirm that I am hitting the controller.

When I hit the controller directly from my browser, the file gets created with 'success' as the content.

When I use cURL from another project to hit the controller, nothing happens, the result is an empty string (I dont care about the result right now, but the file.txt file is no longer being created).

Code that calls the controller is as follows:

$data = array('datetime'=>new DateTime());
        $header = array(
            'Identification:Portal::ReadAPI', // TODO config
            'Content-Type:application/x-www-form-urlencode'
        );
        $url = '[local instance]/admin/orderpicking/get_updated_statuses'; // TODO config

        $curlGetUpdatedItems = curl_init($url);
        /** CURL OPTIONS */
        curl_setopt($curlGetUpdatedItems, CURLOPT_POST, 1);
        curl_setopt($curlGetUpdatedItems, CURLOPT_POSTFIELDS, json_encode(serialize($data)));
        curl_setopt($curlGetUpdatedItems, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curlGetUpdatedItems, CURLOPT_RETURNTRANSFER, true);
//        curl_setopt($curlGetUpdatedItems, CURLOPT_SSL_VERIFYPEER, true);
//        curl_setopt($curlGetUpdatedItems, CURLOPT_SSL_VERIFYHOST, 2);
//        curl_setopt($curlGetUpdatedItems, CURLOPT_CAINFO, $certificate_location);
        /** CURL OPTIONS */

        $result = curl_exec($curlGetUpdatedItems);

Are there any obvious mistakes I'm making here? I will add in a certificate later when I make the portal https but until then I don't think I need it?

Any help and/or advice is extremely welcome!

----- UPDATE -----

After var_dump'ing the result I've noticed that a boolean true is returned. curl_error($curlGetUpdatedItems) returns an empty string.

  • 写回答

1条回答 默认 最新

  • douxiuyu2028 2017-03-14 16:28
    关注

    you can do it make by steps:

    $data_string = http_build_query($data);
    
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($data));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    curl_close($ch);
    
    $result = json_decode($result,true);
    

    don't know if need the header, i think that the default already is application/x-www-form-urlencode

    id it not works, try changing the $data values in array. think helps . :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料